I am using jquery and i am trying to validate a cell phone number or home number with the format ### ### ####. # being a place holder for a digit.
At the moment i am using number but that gives problems with spaces, and i’m using minlength 10 because all SA numbers are min 10 digits.
My code already looks as follows:
// Validation of Form
function validateForm() {
$('#frmMain').validate({
rules: {
TelNumber: { required: true, number: true, minlength: 10 }
},
messages: {
TelNumber: { required: 'Tel number Required' }
},
submitHandler: function (form) {
$('#btnSubmit').click();
}
});
}
Here i found a add on for a US Phone number, if some one could tel me how a US phone number looks maby i can just edit this:
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");
$("#myform").validate({
rules: {
field: {
required: true,
phoneUS: true
}
}
});
1 Answer