-3

can anyone help on how to verify mobile numbers in this script using Ghana number formats. The script validates with only Indian mobile numbers. I want to use ghana mobile numbers to validate the code.

Ghana mobile numbers start with 02 or 05. Thank u.

     pic1 = new Image(16, 16); 
pic1.src = "images/loader.gif";

/*function mobile_validation(mobile_number)
{
    var first_digit = mobile_number.charAt(0);
    var number_length = mobile_number.length;
    if(!isValid(mobile_number, 'numeric'))
        return "Please enter valid mobile number.";
    if(mobile_number.indexOf("+91") != -1 || mobile_number.indexOf("0") == 0 || number_length != 10) //ghana country code is +233
        return "Please enter valid mobile number.";
    if(first_digit != 9 && first_digit != 8 && first_digit != 7) //want to change to 02 or 05
        return "Please enter a valid mobile number.";
    if(mobile_number == "9867045061")
        return "Please enter a valid mobile number.";
    return "valid";
}*/

$(document).ready(function(){

$("#mobile").change(function() { 

var usr = $("#mobile").val();
var first_digit = usr.charAt(0);
var number_length = usr.length;

if(usr.length == 10 && !isNaN(usr) && first_digit == 9 || first_digit == 8 || first_digit == 7) // want to change to 02 or 05
{
$("#statusmb").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Sending Verification Code...');

    $.ajax({  
    type: "POST",  
    url: "verify_mobile.php",  
    data: "mobile="+ usr,  
    success: function(msg){  

   $("#statusmb").ajaxComplete(function(event, request, settings){ 

    if(msg == 'OK')
    { 
        $("#mobile").removeClass('object_error'); // if necessary
        $("#mobile").addClass("object_ok");
        $(this).html('&nbsp;<img src="images/ok.png" align="absmiddle">&nbsp;Verification Code Sent to Mobile');
        document.getElementById('btnSubmit').disabled =false;
    }  
    else  
    {  
        $("#mobile").removeClass('object_ok'); // if necessary
        $("#mobile").addClass("object_error");
        $(this).html(msg);
        document.getElementById('btnSubmit').disabled =true;
    }  

   });

 } 

  }); 

}
else
    {
    $("#statusmb").html('&nbsp;<img src="images/alert.png" align="absmiddle">&nbsp;<font color="red">Please Enter <strong>Valid</strong> Mobile No.</font>');
    $("#mobile").removeClass('object_ok'); // if necessary
    $("#mobile").addClass("object_error");
    }

});

});
user1762269
  • 29
  • 1
  • 5
  • 1
    -1 The validation seem to happen on server-side "verify_mobile.php" ... – Walialu Oct 21 '12 at 12:55
  • Please try to cut out the irrelevant code. Why is the validation function commented out? You should be using a regex to match the mobile number as suggested by NullPointer. – Basic Oct 21 '12 at 12:55
  • See below URL I think it is very help full to you http://stackoverflow.com/questions/11705507/php-validation-of-us-phone-numbers/11705519#11705519 – Abid Hussain Oct 21 '12 at 12:55

3 Answers3

2

You can use regular expression as

var regexMobile = /^[0-9]+$/;
if (mobileLength < 10 || !mobile.match(regexMobile)) {
    document.getElementById("mobile_error").innerHTML = "Enter valid 10 digit Mobile Number";
    return false;
}
Mani
  • 61
  • 5
0

Providing that the format is 02 or 05 followed by an undefined number of numbers. Then this regex should do:

function mobile_validation(mobile_number){
 return mobile_number.match(/^0[2,5]{1}[0-9]+$/) == null? "Please enter a valid mobile number.": "Valid"
}
kabaros
  • 5,083
  • 2
  • 22
  • 35
0

The problem at hand is better handled using regex. The script does a terrible job of even matching the Indian number patterns.

Also, the way it stands, it seems that the script is doing a basic validation on length and the beginning digit and then sending it to the server for further (presumably deeper) validation, as evidenced in the line

url: "verify_mobile.php",

If that is what you are doing too, then you can simply change the line

if(usr.length == 10 && !isNaN(usr) && first_digit == 9 || first_digit == 8 || first_digit == 7) // want to change to 02 or 05

to

if(usr.length == 10 && !isNaN(usr) && first_digit == 0)

assuming the length is 10 for Ghana numbers too.

If you don't want to stick to the script, then use the regex provided in the other answers (which I recommend).

Capstone
  • 2,254
  • 2
  • 20
  • 39