Click here to Skip to main content
14,387,739 members
Rate this:
Please Sign up or sign in to vote.
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>
$("#phone").mask("999-999-9999");
Posted
Updated 16-Jan-17 6:30am
Comments
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
Thanks
Member 13543233 29-Jan-18 6:05am
   
+91-999-999-9999 i want this type of patteren phone number
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
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 - perldoc.perl.org[^]
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.[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100