Home > front end >  how to create regular expression for phone number
how to create regular expression for phone number

Time:04-21

I am trying to create a regular expression that only allow 0-9 and - (). I have no idea how to do that.

I have this /^([\d() -] )$/.But it's allow all digit but i want only 0-9 single digit. I need some idea how to do that.

this are the format i need pass on check

 91 4434117646
 914434117646
 91-4434117646
( 91)4434117646
 91-4434117646
400-544-5568
4005445568
400-544.5568
400)544-5568
400-544-5568

CodePudding user response:

var rx = /^(\(\ ?\d{1,2}\)|\ ?\d{1,2})?[-\s]*\(?\d{3}[\)\s-]?\d{3}[\s\.-]?\d{4}/;

var phone = [" 91 4434117646", " 914434117646", " 91-4434117646", "( 91)4434117646", " 91-4434117646", "400-544-5568", "4005445568", "400-544.5568", "400)544-5568", "400-544-5568"];

phone.forEach(
  element => console.log(element   ' : '   rx.test(element)));

CodePudding user response:

Here is a Regex Cheatsheet https://ihateregex.io/cheatsheet

And here is an example Regex function for phone numbers with an example ^[\ ]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$ https://ihateregex.io/expr/phone/#

  • Related