admin管理员组

文章数量:1291120

I've am using jQuery validation plugin to validate a mobile phone number and am 2/3 of the way there.

The number must:

  • Not be blank - Done,
  • Be exactly 11 digits - Done,
  • Begin with '07' - HELP!!

The required rule pretty much took care of itself and and I managed to find the field length as a custom method that someone had shared on another site.

Here is the custom field length code. Could anyone please suggest what code to add where to also require it begin with '07'?

$.validator.addMethod("phone", function(phone_number, element) {
var digits = "0123456789";
var phoneNumberDelimiters = "()- ext.";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 11;
s=stripCharsInBag(phone_number,validWorldPhoneChars);
return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
}, "* Your phone number must be 11 digits");

function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
$(document).ready(function(){
$("#form").validate();
});

I've am using jQuery validation plugin to validate a mobile phone number and am 2/3 of the way there.

The number must:

  • Not be blank - Done,
  • Be exactly 11 digits - Done,
  • Begin with '07' - HELP!!

The required rule pretty much took care of itself and and I managed to find the field length as a custom method that someone had shared on another site.

Here is the custom field length code. Could anyone please suggest what code to add where to also require it begin with '07'?

$.validator.addMethod("phone", function(phone_number, element) {
var digits = "0123456789";
var phoneNumberDelimiters = "()- ext.";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 11;
s=stripCharsInBag(phone_number,validWorldPhoneChars);
return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
}, "* Your phone number must be 11 digits");

function isInteger(s)
{ var i;
for (i = 0; i < s.length; i++)
{
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}
function stripCharsInBag(s, bag)
{ var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++)
{
// Check that current character isn't whitespace.
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}
$(document).ready(function(){
$("#form").validate();
});
Share Improve this question edited Dec 19, 2011 at 1:30 ThinkingStiff 65.4k30 gold badges147 silver badges241 bronze badges asked Dec 19, 2011 at 0:36 User787665User787665 3375 silver badges23 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

The code in the question seems a very plicated way to work this out. You can check the length, the prefix and that all characters are digits with a single regex:

if (!/^07\d{9}$/.test(num)) {
   // "Invalid phone number: must have exactly 11 digits and begin with "07";
}

Explanation of /^07\d{9}$/ - beginning of string followed by "07" followed by exactly 9 digits followed by end of string.

If you wanted to put it in a function:

function isValidPhoneNumber(num) {
   return /^07\d{9}$/.test(num);
}

If in future you don't want to test for the prefix you can test just for numeric digits and length with:

/^\d{11}$/

You could use this function:

function checkFirstDigits(s, check){
    if(s.substring(0,check.length)==check) return true;
    return false;
}

s would be the string, and check would be what you are checking against (i.e. '07').

Thanks for all the answers. I've managed to e up with this using nnnnnn's regular expression. It gives the custom error message when an incorrect value is entered and has reduced 35 lines of code to 6!

$.validator.addMethod("phone", function(phone_number, element) { 
  return this.optional(element) || /^07\d{9}$/.test(phone_number); 
}, "* Must be 11 digits and begin with 07");

$(document).ready(function(){
        $("#form").validate();
});

Extra thanks to nnnnnn for the regex! :D

Use indexOf():

if (digits.indexOf('07') != 0){
    // the digits string, presumably the number, didn't start with '07'
}

Reference:

  • indexOf().

本文标签: javascriptJQuery Phone Number ValidationStack Overflow