-6

I am trying to validate phone number using regex, and I want to create a phone number format like this:

(+971 12 123 1234)

Can anyone help me do this?

Steve
  • 9,335
  • 10
  • 49
  • 81
C. Patel
  • 1
  • 3
  • i have this type of validation code – C. Patel Dec 01 '15 at 11:19
  • $phone_regex = "^[0-9]{3,4}[\s]{1}[0-9]{3}[-]{1}[0-9]{4}$"; $inputmask = "999 999-9999"; – C. Patel Dec 01 '15 at 11:19
  • And i want to change this +971 12 123 1234 formate – C. Patel Dec 01 '15 at 11:20
  • If you're going to use this for an end-user facing application, I believe that forcing people to write a phone number in a specific format is a bad idea; as long as they give you a valid phone number, then the positioning of the spaces should be irrelevant. – JeffUK Dec 01 '15 at 11:22

2 Answers2

2

Try this way

var phone_regex = /^\(\d{3]\ \d{2]\ \d{3]\ \d{4]\)$/;

More Detail to refer

http://eisabainyo.net/weblog/2009/04/30/10-examples-of-basic-input-validation-in-javascript/

Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0
^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

This will a Help for you.

Help
  • 1,156
  • 7
  • 11