Regular Expression (Regex) for toll free number 1 800 ###-####

Last Reply on Jan 04, 2016 04:29 AM By Shashikant

Posted on Jan 04, 2016 01:05 AM

What is the regular expression for USA toll free number 1 800 ###-####? How to validate this for phone using c#? i want the regular expression only

Posted on Jan 04, 2016 04:29 AM

Please refer below code.

C#

if (System.Text.RegularExpressions.Regex.IsMatch(txtNumber.Text.Trim(), @"^1[\s]?(800)?[\s\-]?\d{3}\-?\d{4}$"))
{
    // this will be valid if phone is 1 800 333-4453
    // this will be valid if phone is 1 800333-4453
    // this will be valid if phone is 1800333-4453
    // this will be valid if phone is 18003334453
    // If you want mandatory space and hyphen in number then use below regex
    // "^1[\s]?(800)[\s]\d{3}\-\d{4}$"
}

VB

If System.Text.RegularExpressions.Regex.IsMatch(txtNumber.Text.Trim(), "^1[\s]?(800)?[\s\-]?\d{3}\-?\d{4}$") Then
         ' this will be valid if phone is 1 800 333-4453
         ' this will be valid if phone is 1 800333-4453
         ' this will be valid if phone is 1800333-4453
	 ' this will be valid if phone is 18003334453
	 ' If you want mandatory space and hyphen in number then use below regex
	 ' "^1[\s]?(800)[\s]\d{3}\-\d{4}$"
End If

I hope this will help you out.