Click here to Skip to main content
14,837,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a textbox to enter phone number,phone number fields are currently free text.The user should be able to enter 10 characters and it should be placed into this format.These formats should also be allowed if the user enters them this way.9-999-999-9999,(999) 999-9999,(999)999-9999.How to do this? which regular expressions could be used for these validation?

What I have tried:

i have tried jquery.maskedinput.js

<script type="text/javascript>
Updated 16-Jan-17 5:30am
Ayodeji Oluwatomiwo 6-Jan-17 11:36am
uhmm can't you just write your own REGEX??

9-999-999-9999 = "\d-\d{3}-\d{3}-\d{4}"
(999) 999-9999 = "(\d{3})\s\d{3}-\d{4}"
(999)999-9999 = "(\d{3})\d{3}-\d{4}"

a simple one tho
Member 11471318 11-Jan-17 5:24am
Thanks for your valuable comment can i check it using one REGEX,How to give mask to a single text field(phone number) which need to allow formats 999-999-9999,9-999-999-999,(999)999-9999
Member 13543233 29-Jan-18 6:05am
+91-999-999-9999 i want this type of patteren phone number

which regular expressions could be used for these validation?

A few tools to help you to build and debug regEx.

Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]

Can you please check the below link:
Matches a phone number with at least 7 digits
Once you open the link kindly click on link Phone number (7+ digits) which is at left panel of screen.

Hope this will help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900