• Resolved lellocoelho

    (@lellocoelho)


    Hi guys,

    I need some help please.

    I would like to insert a single line text field to work as a phone number field.

    I know that the contact form maker has a standard phone number field but this one doesnt work for me.

    So, i would like to know how can i apply a phone number mask in this field as like a jquery or javascript do.

    I found a javascript that do what i want, but i dont know how to include in this field:

    ===== Javascript code ====
    function maskIt(w,m,r,a,p){
    // Variáveis da fun??o
    if (p===true) {
    var txt = (!r) ? w.value.replace(/[^\w]+/gi,”) : w.value.replace(/[^\w]+/gi,”).reverse();
    } else {
    var txt = (!r) ? w.value.replace(/[^\d]+/gi,”) : w.value.replace(/[^\d]+/gi,”).reverse();
    }
    var mask = (!r) ? m : m.reverse();
    var pre = (a ) ? a.pre : “”;
    var pos = (a ) ? a.pos : “”;
    var ret = “”;

    // Loop na máscara para aplicar os caracteres
    for(var x=0,y=0, z=mask.length;x<z && y<txt.length;){
    if((mask.charAt(x)!=’#’) && (mask.charAt(x)!=’&’)){
    ret += mask.charAt(x); x++;
    } else{
    if(mask.charAt(x)==’#’){
    ret += txt.charAt(y).replace(/[^\d]+/gi,”); y++; x++;
    } else if(mask.charAt(x)==’&’) {
    ret += txt.charAt(y).replace(/[\d]+/gi,”); y++; x++;
    }
    }
    }

    // Retorno da fun??o
    ret = (!r) ? ret : ret.reverse()
    w.value = pre+ret+pos;
    }

    // Novo método para o objeto ‘String’
    String.prototype.reverse = function(){
    return this.split(”).reverse().join(”);
    };
    function maskCel (obj) {
    var str = obj.value;
    var ddd = str.substring(1,3);
    var pdi = str.substring(5,6);
    // ORIGINAL – Mudei tirando o DDD if ((ddd == 11) && (pdi == 9)) {
    if (pdi == 9) {
    maskIt(obj,'(##) #.####-####’, false, “”, true);
    } else {
    maskIt(obj,'(##) ####-####’, false, “”, true);
    }
    }

    ============== HTML code to call JS ===

    <input type=”text” name=”placa” onkeyup=”maskCel(this)” />

    ================

    To see how it works just copy and paste the code in this website:
    https://codepen.io/pen/

    thank you for your help.

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Support Suzie

    (@suziekh)

    Hi,

    Please, go to Form > Settings > before_load function

    Add the following code

    jQuery("#wdform_10_element1").on("keypress keyup blur",function (event) {
    
             //jQuery(this).val(jQuery(this).val().replace(/[^d].+/, ""));
             if ((event.which < 48 || event.which > 57)) {
                  event.preventDefault();
             }
        }
                                        );

    Write input ID instead of wdform_10_element1

    Thank you and have a nice day!

    Thread Starter lellocoelho

    (@lellocoelho)

    Hi Suzie,

    Thank you for your help.
    Can you tell me where do I set the id name on the field please?
    Because when I edit the field it doesnt have the id option.
    This is the options that I have to edit:

    Single Line Text:
    Label
    Label position
    Hide label
    Required
    Placeholder
    Width(px)

    ADVANCED OPTIONS
    Label width(px)
    Readonly
    Allow only unique values
    Validation (RegExp.)
    Class name
    Additional Attributes

    Thank you so much! ??

    Plugin Support Suzie

    (@suziekh)

    Hi,

    Here the ID of that field is > wdform_12_element1

    If you want to see it, right click on the input, press inspect and you will see the following element

    <input type="text" class="wd-width-100" id="wdform_12_element1" name="wdform_12_element1" value="" title="" placeholder="">

    Here you can see the ID.

    Thanks and have a nice day!

    Thread Starter lellocoelho

    (@lellocoelho)

    Hi Suzie!

    Sorry to bother you again.

    Awesome!! Thank you very much for your help.
    I thought that I could change and set my ouwn field id direclty on the contact form maker aplication.

    But I have another problem with the code you suggested… Its not working ??

    The code like you wrote dont do nothing. But I saw that in your code has one line commented.

    //jQuery(this).val(jQuery(this).val().replace(/[^d].+/, ""));

    When I uncomment this line your code do something, but its not what I expected.

    I can input only 2 numbers and it erases automatically.

    I really dont know where am I wrong.

    This is the code as like I wrote:

    
    jQuery("#wdform_12_element1").on("keypress keyup blur",function (event) {
    
             jQuery(this).val(jQuery(this).val().replace(/[^d].+/, ""));
             if ((event.which < 48 || event.which > 57)) {
                  event.preventDefault();
             }
        }
                                        );
    
    

    Thank you in advance

    • This reply was modified 5 years, 8 months ago by lellocoelho.
    • This reply was modified 5 years, 8 months ago by lellocoelho.
    • This reply was modified 5 years, 8 months ago by lellocoelho.
    • This reply was modified 5 years, 8 months ago by lellocoelho.
    Thread Starter lellocoelho

    (@lellocoelho)

    Hi Suzie,

    I take a look more deeply and I found this code:

    could you please take a look why this code doesnt work only on CFM?
    this code do exactly what I need.

    I did some tests with this code on playcode.io and codepen.io and it works properly. But when I paste it on the CFM the code fails.

    I commented it in english for you.

    function before_load() {
         $("#wdform_12_element1").on("keypress keyup",function(){
              // pego o valor do telefone e coloco na variavel
              // I get the value of the field and put it on a VAR
              var texto = $(this).val();
              // Tiro tudo que n?o é numero da variavel
              // I take off the var all letters and leave only numbers
              texto = texto.replace(/[^d]/g, '');
              // Se tiver alguma coisa digitada
              // If there is a number
              if (texto.length > 0)
              {
                   // Ponho o primeiro parenteses do DDD
                   // I insert the first '(' of the DDD    
                   texto = "(" + texto;
                   
                   //pergunto se digitou 3 numeros
                   // If it has more than 3 numbers
                   if (texto.length > 3)
                   {
                        // Fecha o parenteses do DDD com 2 numeros
                        // I insert the second ')' of the DDD with 2 numbers
                        texto = [texto.slice(0, 3), ") ",texto.slice(3)].join('') ;
                        
                   }
                   if (texto.length > 12)
                   {
                        
                        // Se for 13 digitos ( DDD + 9 digitos) ponhe o tra?o no quinto digito
                       // If it has 13 digits (DDD plus 9 numbers) I insert the '-' on the 5 digit            
                        if (texto.length > 13) 
                             texto = [texto.slice(0, 10), "-", texto.slice(10)].join('');
                        else
                             // Se for 12 digitos ( DDD + 8 digitos) ponhe o tra?o no quarto digito
                      // If it has 12 digits (DDD plus 8 numbers) I insert the '-' on the 4 digit
                             texto = [texto.slice(0, 9), "-", texto.slice(9)].join('');
                   }
                   
                   // N?o adianta digitar mais digitos!
                  // dont allow to insert more numbers
                   if (texto.length > 15)                
                        texto = texto.substr(0,15);
              }
              // Retorna o texto
              $(this).val(texto);
              
         }
                                    )    
    }
     

    Thank you for your help.

    Have a nice day! ??

    • This reply was modified 5 years, 8 months ago by lellocoelho.
    Plugin Support Suzie

    (@suziekh)

    Hi,

    Please, add the code to before_load function.

    Remove the modification added by you, it brings JS error.

    Check this, please.

    Thread Starter lellocoelho

    (@lellocoelho)

    Hi Suzie,

    I saw the video that you recorded and the test that you did not brings what I need to do…

    I think that I didn’t explain very well what I really need to do….

    I need to mask the numbers imputed in this field like this way:
    Numbers start with 9 mask this :
    (11) 9.1234-5678

    Numbers start with any number except 9 mask this:

    (11) 1234-5678

    What I expect the field do is to bring the right format when someone input only numbers.

    Example: one client input the numbers 11987654321 the field brings the () . and – and modify for this format (11) 9.8765-4321
    This is the format of mobile numbers from Brazil. And I need to do the same with local numbers who doesn’t start with 9 and have one digit less.

    Thank you

    Plugin Support Suzie

    (@suziekh)

    Hi,

    We can do this way.

    This part (11) 9. can be already displayed on the form, and the users will add the rest of the numbers.

    Is it ok for you?

    Thanks!

    Thread Starter lellocoelho

    (@lellocoelho)

    Hi,

    Sorry but this not gonna work for me. Let me explain:

    (11) -> this part is where the customer input the country. In Brazil we have a lot of numbers like:
    11- S?o Paulo
    21- Rio de janeiro
    61 – Brasilia
    etc.

    The part 9. means that is a mobile phone. Every mobile number starts with 9 in Brazil and have 1 digit more then a local phones. This is why after the number 9 we have a dot.

    EX: 9.6789-7374 => 9 digits

    But we have local phones too starting from 1 to 8 (9 is only for mobiles) and have 1 digit less then a mobile phones:

    EX: 5631-5055 => 8 digits

    So, what I need to the field do is:

    1- not allow to input letters like your code do.
    2- Mask mobile numbers like I said on the last message.

    Thanks.

    Plugin Support Suzie

    (@suziekh)

    Dear @lellocoelho,

    This is a customization and we are sorry to let you know that we do not provide it for free plugins.

    Thanks for your time and have a nice day!

    Thread Starter lellocoelho

    (@lellocoelho)

    Hi Suzie,

    I′m sorry but I disagree with you. Its not a customization… Its only a mobile and local phone mask, nothing more.

    I dont understand so much of javascript and, after some nights trying, I′ve found a way.

    First step you need to download and paste jquery into JS folder of your theme

    Second step right click into your browser and click into “inspect” (ctrl+shift+I)
    – open the console and take a look of the errors that show to you.

    then this is the code that′s worked for me:
    This code can be improve of course, but it already do what I need.
    ———————————————————————
    Portuguese

    Pessoal tive um problema para usar o Contact Form Maker.
    Existe uma field padrao para telefone ja propria do formulario, porem ela nao fornece a mascara de telefones locais e celulares do Brasil.

    Depois de muito quebrar a cabeca e com um pouco de ajuda da Suzie eu acabei conseguindo resolver a questao pegando alguns codigos da internet e fazendo testes.
    Vou explicar como resolvi.

    Primeiro quando abri o site do formulario eu cliquei com o bot?o direiro do mouse e cliquei em inspecionar. Fui na aba console e la me mostrou um erro de Jquery.

    O arquivo Jquery nao estava na pasta JS do tema que usava. fiz o download e upei ele para a pasta em wp-content/themes/serif-pro (no meu caso) /js
    O caminho que estava faltando foi informado no console do browser.

    Apos feito isso ai vieram as noites sem dormir pegando codigos e mais codigos na internet para fazer a mascara funcionar.
    No final ficou esse codigo abaixo.
    Com certeza da pra melhorar, mas ele ja faz aquilo que eu precisava.

    OBS: eu precisava que o telefone fixo ficasse no formado (11)5665-5555 e o celular no formato (11)9.6666-7878

    Segue.

    
    
    
    
    function before_load() 
    {
         
         jQuery("#wdform_12_element1").on("keypress keyup",function(){
              // pego o valor do telefone e coloco na variavel
              var texto = jQuery(this).val();
              
              if (texto.length == 0) {
                   texto = texto +"(";
              }
              
              if (texto.length == 3) {
                   texto = texto +")";
              }
              
              if (texto.length == 5)  {
                   if (texto.includes('9')) {
                        texto = texto +"." ;
                   }
                   
              }
              
              
              if (texto.length == 12) {
                   
                   texto = [texto.slice(0, 8), "-", texto.slice(8)].join('');
              }
              
              
              if (texto.length == 15) 
                   texto = [texto.slice(0, 8), texto.slice(9, 11), "-", texto.slice(11) ].join('');
              
              
              
              jQuery(this).val(texto);
              
         }
                                         ) 
              
    }
    
    • This reply was modified 5 years, 7 months ago by lellocoelho.
    • This reply was modified 5 years, 7 months ago by lellocoelho.
    Plugin Support Suzie

    (@suziekh)

    Hi,

    Please, delete the code you have added and add the one below. Please, add it to the JS of Form Maker, at the end, when our functions end.

    jQuery(document).ready(function(){

    jQuery(“#wdform_2_element10”).on(“keypress keyup”,function(event){
    var regExp = new RegExp(‘[a-zA-Z]’);

    var value = jQuery(this).val();

    // Do not allow alphabets to be entered.
    if (regExp.test(value)) {
    event.preventDefault();
    return false;
    }
    var texto = jQuery(this).val();
    var selected = bwg_get_textarea_selection(“wdform_2_element10″);
    texto = texto.replace(selected, ”);
    if (texto.length == 0 && event.keyCode != 8) {
    texto = texto +”(“;
    }

    if (texto.length == 3 && event.keyCode != 8) {
    texto = texto +”)”;
    }
    if (texto.length == 5 ) {
    if (texto.includes(‘9’) && texto[4] == 9 && event.keyCode != 8) {
    texto = texto +”.” ;
    jQuery(this).removeAttr(‘maxlength’);
    jQuery(this).attr(‘maxlength’, ’15’);
    jQuery(this).removeClass(‘local’).addClass(‘mobile’);
    }else{
    jQuery(this).removeAttr(‘maxlength’);
    jQuery(this).attr(‘maxlength’, ’13’);
    jQuery(this).removeClass(‘mobile’).addClass(‘local’);
    }
    }
    if(jQuery(this).hasClass(‘mobile’)){
    if(texto.length ==10 && event.keyCode != 8){
    texto = texto +”-” ;
    }
    }
    if(jQuery(this).hasClass(‘local’)){
    if(texto.length ==8 && event.keyCode != 8){
    texto = texto +”-” ;
    }
    }

    jQuery(this).val(texto);

    }
    )

    })
    function bwg_get_textarea_selection(id) {
    var textComponent = top.document.getElementById(id);
    var selectedText;
    if (textComponent.selectionStart !== undefined) {
    /* Standards Compliant Version */
    var startPos = textComponent.selectionStart;
    var endPos = textComponent.selectionEnd;
    selectedText = textComponent.value.substring(startPos, endPos);
    }
    else if (document.selection !== undefined) {
    /* IE Version */
    textComponent.focus();
    var sel = document.selection.createRange();
    selectedText = sel.text;
    }
    return selectedText;
    }

    Please, add your ID to the code.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Contact Form Maker Masks’ is closed to new replies.