Regular expressions are a good way to validate text fields such as names, addresses, phone numbers, and other user information. You can use them to constrain input, apply formatting rules, and check lengths.
You can use the System.Text.RegularExpressions.Regex class for validate any input string for any specific format.
A regular expression can easily check whether a user entered something that looks like a valid phone number. By using capturing groups to remember each set of digits, the same regular expression can be used to replace the subject text with precisely the format you want.
The following example will allow you to Input Phone Number only.
In this example we can determine whether a user entered phone number in a common format that includes 0123456789, 012-345-6789, (012)-345-6789, (012)3456789 012 3456789, 012 345 6789, 012 345-6789, (012) 345-6789, 012.345.6789 and all related combinations
[C#]
using System.Text.RegularExpressions; public static bool CheckNumber(string strPhoneNumber) { string MatchPhoneNumberPattern = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"; if (strPhoneNumber!= null) return Regex.IsMatch(strPhoneNumber, MatchPhoneNumberPattern ); else return false; }
[VB.NET]
Import System.Text.RegularExpressions Public Shared Function CheckPhoneNumber (ByVal strPhoneNumber As String) As Boolean Dim MatchPhoneNumberPattern As String = "^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$" If strPhoneNumber IsNot Nothing Then Return Regex.IsMatch(strPhoneNumber , MatchPhoneNumberPattern) Else Return False End If End Function
Similarly if we want to validate phone number like xxx-xxx-xxxx format then you can use “\d{3}-\d{3}-\d{4}”.
Regular expression for phone numbers with country code:
If you want to use regular expression for validating phone or mobile numbers followed by country code. For example if you want to validating Indian mobile numbers followed by +91, then you can use the following regx:
(\+91[\-\s])\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$
Now i am simplifying the above regular expression as ^(\+91[\-\s]?)\d{10}$.
————————————————————-
Edited by Hirendra Sisodiya on 2 June, 2012.
Edited by Hirendra Sisodiya on 15 March, 2012.
Edited by Hirendra Sisodiya on 11 Jan, 2012
Hi Ankur,
Your post was very helpful..
Can i get reg expression for following two formats plzz…
1. +91 then followed by 10 digit no
2. 080-then followed by 8 digit no
Hi Ankur,
Your post was very helpful..Can i get reg expression for following two formats plzz…1. +91 then followed by 10 digit no2. 080-then followed by 8 digit no
hello Munika
I have updated this article by adding your request, please see in the end of the article.
hey friends this is really helpful 😀 but do anyone guide me for email Id validation plz?
hi bhusan
you can search the topic from the ‘Search’ for any requirment
however you should read the following:
http://www.authorcode.com/regular-expression-for-validating-email-in-net/
what will be the method if i want to put (-) this symbol after first four character the next seven digits will be written in aspx file, similarly i want to put (-) in syntax formate for CNIC how i write the regular expression
i want to set (xxxx-xxxxxxx) this formate for Cell number in .NET and (xxxxx-xxxxxxxx-x) this formate for CNIC how can i set this formate using aspx