Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example of a regular expression in jquery for phone numbers

I'm very new to javascript, I just want a reqular expression for validating phone numbers for one of my text field.

I can accept -+ () 0-9 from users, do you have regex for this one or a regex for phone numbers better then the one i need?

Thanks in advance.

like image 348
Pawan Choudhary Avatar asked Aug 05 '11 17:08

Pawan Choudhary


People also ask

How can I check mobile number in regex?

/^([+]\d{2})? \d{10}$/ This is how this regex for mobile number is working. + sign is used for world wide matching of number.

How can I validate a phone number in JavaScript?

Validate a Phone Number Using a JavaScript Regex and HTML function validatePhoneNumber(input_str) { var re = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/; return re. test(input_str); } function validateForm(event) { var phone = document. getElementById('myform_phone').

What is regular expression in jQuery?

It's used to match strings or parts of strings and for search and replace operations. jQuery uses regular expressions extensively for such diverse applications as parsing selector expressions, determining the type of browser in use, and trimming whitespace from text.


2 Answers

Use this rexeg

/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/
  • (123) 456 7899
  • (123).456.7899
  • (123)-456-7899
  • 123-456-7899
  • 123 456 7899
  • 1234567899

supported

like image 144
genesis Avatar answered Oct 23 '22 05:10

genesis


Try this

function validatePhone(phoneNumber){
   var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
   return phoneNumberPattern.test(phoneNumber); 
}
like image 20
ShankarSangoli Avatar answered Oct 23 '22 06:10

ShankarSangoli