Javascript regex - what to use to verify a phone number?

Can someone tell me that RegEx will work to check the international phone number, including the space between the numbers, as well as for these characters: - ( ) .

The number of numbers in a line is not too important, I would like the user to be able to enter something like Example 1 or 2 if they wish:

Example:

  • +44 (0) 207 111 1111

  • 442071111111

I have already read and tested the published answers to some of these similar questions, as far as I know, but so far none of them work for me the way I want them.

Please can someone help me with a clear explanation of how this should be written for confirmation?

Many thanks to everyone who can help.

+7
source share
6 answers
 try this $("#phone").blur(function () { var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/ var no = $("#phone").val(); if (!regexp.test(no)&& no.length<0) { alert("Wrong phone no"); } }); <input type="text" id="phone"/> 
+12
source

See Full Regular Expression to Verify Phone Number

Quick cheat sheet

  • Run the expression: /^
  • If space is required, use: [\s] or \s
  • If you want to require brackets, use: [(] and [)] . Using \( and \) is ugly and can be confusing.
  • If you want something to be optional, put it ? after him
  • If you want a hyphen, just type - or [-] . If you do not put it first or last in a series of other characters, you may need to avoid it: \-
  • If you want to accept different options in the slot, put the brackets around the parameters: [-.\s] a hyphen, period or space is required. The question mark after the last bracket makes all of them optional for this slot.
  • \d{3} : A 3-digit number is required: 000-999. Abbreviation for [0-9][0-9][0-9] .
  • [2-9] : The number 2-9 is required for this slot.
  • (\+|1\s)? : Accept plus or 1 and a space (pipe symbol, | , "or") and make it optional. The plus sign must be escaped.
  • If you want certain numbers to match the slot, enter them: [246] will need 2, 4 or 6. [77|78] will need 77 or 78.
  • $/ : End expression
+10
source

This is a long regular expression, but it supports both formats (for example, 2 for a valid international number, MUST start with + or 00):

 /^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/i 

This allows you to use extensions and multiple choice of formats and separators.

Matches:

  • (+ 351) 282 43 50 50
  • 90191919908
  • 555-8909
  • 001 6867684
  • 001 6867684x1
  • 1 (234) 567-8901
  • 1-234-567-8901 x1234
  • 1-234-567-8901 ext1234
  • 1-234 567.89 / 01 ext. 1234
  • 1 (234) 5678901x1234
  • (123) 8575973
  • (0055) (123) 8575973

In $ n, it saves:

  • Country indicator
  • Phone number
  • Extension

The same answer was given here: Full regex for checking phone number (direct link to my answer)

+7
source
 /* @isValidUSPhoneFormat function will check valid US Format Allowed US Format (123) 456-7890 123-456-7890 123.456.7890 1234567890 (734) 555.1212 */ function isValidUSPhoneFormat(elementValue){ var phoneNumberPattern = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s.]{0,1}[0-9]{3}[-\s.]{0,1}[0-9]{4}$/; if(phoneNumberPattern.test(elementValue) == false) { var phoneNumberPattern = /^(\()?\d{3}(\))?(.|\s)?\d{3}(.|\s)\d{4}$/; return phoneNumberPattern.test(elementValue); } return phoneNumberPattern.test(elementValue); } 

Let this help you understand JavaScript RegEx ..

+1
source

Do not even try. Trying to protect against what you think is unacceptable can lead to angry users who cannot enter perfectly valid phone numbers. And if the user really wants to enter the wrong phone number, he / she will be able to do it anyway.

+1
source
 function checkPhoneNumber(val) { var num = document.getElementById(val).value; var mob=/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g; if (mob.test(num) == false) { alert("Please Enter Valid Phone Number."); document.getElementById(val).value = ""; return false; } if (num.length > 15) { alert("Only 15 characters allowed for Phone Number field."); document.getElementById(val).value = ""; return false; } return true; } 

Try

+1
source

All Articles