Reputation: 201
I have set up validation for a HTML form using jQuery and I'm looking for some help regarding the phone number validationy, I have been searching online and here's what I came up with but its still not working! I have the email and name inputs validating but cant figure out how to implement the phone number validation correctly as its still accepting letters when I submit? I need the input to only accept a maximum of 15 numbers and also accept the + sign at the start if neccessary?
The regEx I used is: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/ and I'm aware this isn't correct but I cant even get this to work?
Any input on how to fix this problem much appreciated!
$('#submit_second').click(function(){
//remove classes
$('#second_step input').removeClass('error').removeClass('valid');
var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
var phonePattern = /^[0-9]{3}\-[0-9]{7}$/ ;
var fields = $('#second_step input[type=text]');
var error = 0;
fields.each(function(){
var value = $(this).val();
if( value.length<1 || value==field_values[$(this).attr('id')] || ( $(this).attr('id')=='email' && !emailPattern.test(value) ) ) {
$(this).addClass('error');
$(this).effect("shake", { times:3 }, 50);
error++;
} else {
$(this).addClass('valid');
}
if( value.length<1 || value==field_values[$(this).attr('id')] || ( $(this).attr('id')=='phone' && !phonePattern.test(value) ) ) {
$(this).addClass('error');
$(this).effect("shake", { times:3 }, 50);
error++;
} else {
$(this).addClass('valid');
}
});
Upvotes: 1
Views: 2098
Reputation: 91892
This should do it:
/^\+?[0-9]{0,15}$/
This matches up to 15 numbers, optionally prefixed with +
Upvotes: 2