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"> 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(' <img src="images/ok.png" align="absmiddle"> 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(' <img src="images/alert.png" align="absmiddle"> <font color="red">Please Enter <strong>Valid</strong> Mobile No.</font>');
$("#mobile").removeClass('object_ok'); // if necessary
$("#mobile").addClass("object_error");
}
});
});
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
If that is what you are doing too, then you can simply change the line
to
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).