Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    US Telephone number validation fails

    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!

    Code:
    (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);

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    EDIT
    =======
    removed my attempt since after trying original reg ex I couldn't find(see) anything wrong with it.

    sorry
    LT
    Last edited by low tech; 12-16-2010 at 04:52 AM.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •