www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Regex form validation phone numbers

  1. #1
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293

    resolved [RESOLVED] Regex form validation phone numbers

    can someone help me get this to validate correctly. or improve on it

    it works fine except i can type letters and the form still goes through. i want the option of xxx-xxx-xxxx or (xxx) etc.

    Code:
    if (!phone.match(/^[2-9]\d{2}\-\d{3}\-\d{4} | ^\({1}[2-9]\d{2}\){1}\s?\d{3}\-\d{4} | ^[2-9]\d{2}\s\d{3}\s\d{4}/)) {
    			valid += '<p>Your Phone Number & Area Code are  is required</p>';												  
    		}
    		
    		if (phone.trim().length === 0) {
    			valid += '<p>Please enter your phone number with the area code</p>';
    	    	}

  2. #2
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    here is the answer to the question if anyone else needs it...


    Code:
    if (!phone.match('^(([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+$')) {
    			valid += '<p>Your Phone Number & Area Code are  is required</p>';												  
    		}

  3. #3
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    and the php check

    Code:
      $reg_phone = "/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/";  
    
    
    if (!preg_match($reg_phone, $phone)) {
    				   
    				    $error_message .= "<p>Please provide your phone number.</p>";			   
    		}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles