Skip to:
Content
Pages
Categories
Search
Top
Bottom

Phone Number Validation Not Working


  • slash75
    Participant

    @slash75

    I have a field set as Phone Number type but I am able to put anything into the field and it does not validate as a phone number. Has anyone else run into this are know what the issue may be?

Viewing 9 replies - 1 through 9 (of 9 total)

  • shanebp
    Moderator

    @shanebp

    The Phone Number field type does not validate for numbers.
    So it is working as it should.
    You will need to write your own validation code for that field.


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    You can check tutorials like this http://www.jquerybyexample.net/2012/05/validate-phone-numbers-using-jquery.html and can try this code as well.

    Thanks


    slash75
    Participant

    @slash75

    Thanks for the link. Where would I add this code?


    Prashant Singh
    Participant

    @prashantvatsh

    You can try in bp-custom.php or child theme’s functions.php. If not sure about files then use this plugin https://wordpress.org/plugins/code-snippets/ to add snippets.

    Thanks


    slash75
    Participant

    @slash75

    I added the following code to the bp-custom.php file but unfortunately it did not work.

    
    <?php
    
    $(document).ready(function() {
        $('#field_29').blur(function(e) {
            if (validatePhone('field_29')) {
                $('#spnPhoneStatus').html('Valid');
                $('#spnPhoneStatus').css('color', 'green');
            }
            else {
                $('#spnPhoneStatus').html('Invalid');
                $('#spnPhoneStatus').css('color', 'red');
            }
        });
    });
    
    function validatePhone(field_29) {
        var a = document.getElementById(field_29).value;
        var filter = /^[0-9-+]+$/;
        if (filter.test(a)) {
            return true;
        }
        else {
            return false;
        }
    }
    
    ?>
    

    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    This is jquery code. Please remove it from there and use this plugin https://wordpress.org/plugins/custom-css-js/. Add a new js code and then paste this code.

    Thanks


    slash75
    Participant

    @slash75

    I tried this as well but it did not work.

    Is there any reason that The Phone Number field type does not validate phone numbers?


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    Please replace all the ‘$’ with ‘jQuery’ save the js code and then on frontend open browser console(press ctrl+shift+I if you are on chrome) and then check if there is any error in the console.

    Thanks


    slash75
    Participant

    @slash75

    I updated the code with what is below but it still is not working.

    `jQuery(document).ready(function() {
    jQuery(‘#field_29’).blur(function(e) {
    if (validatePhone(‘field_29’)) {
    jQuery(‘#spnPhoneStatus’).html(‘Valid’);
    jQuery(‘#spnPhoneStatus’).css(‘color’, ‘green’);
    }
    else {
    jQuery(‘#spnPhoneStatus’).html(‘Invalid’);
    jQuery(‘#spnPhoneStatus’).css(‘color’, ‘red’);
    }
    });
    });

    function validatePhone(field_29) {
    var a = document.getElementById(field_29).value;
    var filter = /^[0-9-+]+jQuery/;
    if (filter.test(a)) {
    return true;
    }
    else {
    return false;
    }
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar