简体   繁体   中英

phone number validation in check_phone function

I have a signup.php file with a script that does validation for username and email. I have added a phone number field in the php file and added it in the database too as varchar(15). Everything is working fine but it accepts any crap entered in the field. What syntax should I add in the below script to validate phone number?

Required validation *The textbox should allow 11 digit number as input. *If user enters a 10 digit number, in the database it should update the database table field as +91 followred by the 10 digit number *If user enters a 11 digit number starting with 0, it should remove the zero and update the database table field as +91 followred by the 10 digit number (removing the 0) *alphabets and special character not allowed

<script type="text/javascript">
function check_username(){
    var b=$('#username').val();
    if(b.length<3){
        $('#username').css('border','2px solid #a50000')
    }else{
        $.get("system/ajax.php?a=checkUser",{data:b},function(a){
            if(a==1){
                $('#username').css('border','2px solid #00a500')
            }else{
                $('#username').css('border','2px solid #a50000')
            }
        })
    }
}
function check_email(){
    var b=$('#email').val();
    if(b.length<3){
        $('#email').css('border','2px solid #a50000')
    }else{
        $.get("system/ajax.php?a=checkEmail",{data:b},function(a){
            if(a==1){$('#email').css('border','2px solid #00a500')
            }else{
                $('#email').css('border','2px solid #a50000')
            }
        })
    }
}
function check_email2(){
    var a=new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    var b=$('#email').val();
    var c=$('#email2').val();
    if(!a.test(c)){
        $('#email2').css('border','2px solid #a50000')
    }else if(b==c){
        $('#email2').css('border','2px solid #00a500')
    }else{
        $('#email2').css('border','2px solid #a50000')
    }
} 
</script>

Thank you

First of all, your primary validation should be done in PHP, not in JavaScript. Even if you fix the JavaScript code, any visitor who comes along can just disable JavaScript and enter anything they want in the fields.

To validate the phone number in PHP, try the following:

<?php
function validatePhoneNumber($num) {
    if (!ctype_digit($num)) {
        return false;
    }
    $len = strlen($num);
    if ($len === 11) {
        if ($num[0] === "0") {
            return "+91" . substr($num, 1);
        } else {
            return $num;
        }
    } else if ($len === 10) {
        return "+91" . $num;
    } else {
        return false;
    }
}
?>

The function validatePhoneNumber will return false if the phone number is invalid. Otherwise, it will return the parsed phone number as a string.

Use it like this:

<?php
validatePhoneNumber("4455");        // false
validatePhoneNumber("(55)5555555"); // false
validatePhoneNumber("5518843371");  // "+915518843371"
validatePhoneNumber("04113313311"); // "+914113313311"
validatePhoneNumber("14355555555"); // "143555555555"
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM