...

View Full Version : US Telephone number validation fails



stefpist
12-15-2010, 11:18 PM
Hello,

I have the following code that, among other things, should validate US telephone numbers with format: "(800) 800-8000" with hyphen, parenthesis and spaces optional. The regexp is: /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/

My main problem is that it validates also numbers like 800 or 800000000000000 etc.
The code works perfectly on test pages but not on my website.
Something similar happens also for the zipcode.

Can someone please tell me what could be wrong?
Thanks!


(function($){
$.fn.ax_validate = function(f){
stopAnim(); // just too much otherwise
//console.log(typeof f + ', id = ' + f.id);
var n, el, err = [], msg = [],
fmats = {
'email': /^[\w\.\-]+\x40[\w\.\-]+\.\w{2,4}$/,
'phone': /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/,
'zip': /^\d{5}(-\d{4})?$|[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d/
};

$('input[type=text],textarea').each(function(i){
$(this).val( $.trim($(this).val()) );
});

$('label.req').each(function(i){
n = $(this).attr('for');
el = $('input[name=' + n + ']');

if (typeof fmats[n] != 'undefined' && !el.val().match(fmats[n])) {
err.push(n);
msg.push($(this).text() + ((el.val() == '') ? '' : ' (invalid format)'));
$(this).animate({color: '#A60'}, 1000);

} else if (el.val() == '') {
err.push(n);
msg.push($(this).text());
$(this).animate({color: '#A00'}, 1000);

} else {
$(this).css('color', '#0f4068');
}
});
if (err.length == 0) {
//alert('All ok!');
//f.submit();
return true;
}
alert('<b>Please fix the following required fields:</b><br /><br />' + msg.join('<br />'));
//$('input[name=' + err[0] + ']').focus();
return false;
};
})(jQuery);

low tech
12-16-2010, 02:59 AM
EDIT
=======
removed my attempt since after trying original reg ex I couldn't find(see) anything wrong with it.

sorry
LT

Philip M
12-16-2010, 08:56 AM
Nothing wrong with the regexes.

if (typeof fmats[n] != 'undefined' && !el.val().match(fmats[n])) {


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum