• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Validating ZipCode,Phone Number and Address

1.I need to validate the zipcode entered by user not only for digits but also whether it exits or not If the user enters 00001 if it is not existing zipcode and   error message need to be shown.
Can you please give me any regular expression for validating the  zipcode whether it exists or not.
2.Validating area codes and exchange codes based on NANPA standards. I am validating the second 3 digits in Phone number as follows:First digit can be 2 to 9.It can' be 0 or 1. Is there any exceptions for this?
3.The address can be alphabets only or if numbers is entered it should have alphabets also.

Your help is appreciated.
0
KavyaVS
Asked:
KavyaVS
  • 13
  • 12
1 Solution
 
käµfm³d 👽Commented:
>>  Can you please give me any regular expression for validating the  zipcode whether it exists or not.

You should not try to validate existence of a zip code using regex; you can only validate whether it fits some structure. You might be able to construct a pattern to do this, but maintenance would be a nightmare if USPS removed or added any zip codes (which as I understand does happen).


>>  Validating area codes and exchange codes based on NANPA standards.

This is one I created from the information found at:  http://en.wikipedia.org/wiki/North_American_Numbering_Plan

^\s*[2-9][0-8]\d[2-9]\d{6}\s*$

Open in new window



>>  The address can be alphabets only or if numbers is entered it should have alphabets also.

I'd think you would want spaces too, but if not, simply remove the space from the bracket epressions (last character in each bracket):
[a-zA-Z ]+|[a-zA-Z0-9 ]+

Open in new window

0
 
käµfm³d 👽Commented:
P.S.

Here is a pattern for zip-code structure with the possibility of +4:
\d{5}(?:-\d{4})?

Open in new window

0
 
KavyaVSAuthor Commented:
Thanks for your reply.

Atleast 2 alphabets should be in the address.
How to modify this regular expression [a-zA-Z ]+|[a-zA-Z0-9 ]+

Thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
käµfm³d 👽Commented:
>>  Atleast 2 alphabets should be in the address.

I see now that that pattern was incorrect anyway. Let's try this, with the new requirment added:
(?=.*?[a-zA-Z].*?[a-zA-Z])[a-zA-Z0-9 ]+

Open in new window

0
 
KavyaVSAuthor Commented:
I tried this regular expression "(?=.*?[a-zA-Z].*?[a-zA-Z])[a-zA-Z0-9,'#- ]+"

I entered 783,David Fairleigh Ct Apt#1. But it is showing error message.Not a valid address.

How to change this expression to make it work "(?=.*?[a-zA-Z].*?[a-zA-Z])[a-zA-Z0-9,'#- ]+"


Thanks
0
 
käµfm³d 👽Commented:
Put the hyphen as the last (or first) character in the character class:
(?=.*?[a-zA-Z].*?[a-zA-Z])[a-zA-Z0-9,'# -]+

Open in new window

0
 
KavyaVSAuthor Commented:
The address is working now.
Regular expression for Phone number: "^\(?([2-9][0-8][0-9])\)?([2-9][0-9]{2})?([0-9]{4})$"
In area code(first 3 digits)last 2 digits can't be 11.It shouldn't allow 911 or 711.

How to change the above expression so that last 2 digits of area code shouldn't be 11.

Thanks
0
 
käµfm³d 👽Commented:

"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})$"

Open in new window

0
 
KavyaVSAuthor Commented:
Hi kaufmed,
Thanks.It is working now.
I am using the same Regular expression for Business phone number. But Business phone no can have extension,so I need to specify the range of digits 10 to 14.

How to modify this expression to include the range of digits and the user can enter only digits.
"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})$"
0
 
käµfm³d 👽Commented:

"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$"

Open in new window

0
 
KavyaVSAuthor Commented:
It is working. But there is one flaw in it. I have 4 text boxes for entering the Businees Phone number.
One TextBox for Areacode,2nd TextBox for Exchange Code,3rd Textbox for 4 digits and 4th Text Box for extension.I set the max length property for TextBoxes.
But when entered 2 digits in 3rd TextBox and 2 digits in 4th Text Box it is returning true.
Same thing when I entered only one digit in area code TextBox and remaining 2 digits in 4th text box becoz the total no.digits equal to 10.
How to overcome this.The user has to enter 3 digits in 1st and 2nd Text Boxes and 4 digits in 3rd Text Box.

Thanks
0
 
käµfm³d 👽Commented:
That pattern is designed to validate one string (i.e. one TextBox). If you want to validate 4 TextBoxes, you'll need four RegularExpressionValidators--one for each box--and you'll need to match the appropriate pattern to each box.

The break-up would be this:

Area Code
\(?[2-9][0-8][0-9](?<!11)\)?

Open in new window


Exchange
[2-9][0-9]{2}

Open in new window


Subscriber
[0-9]{4}

Open in new window


Extension
[0-9]{0,4}

Open in new window

0
 
KavyaVSAuthor Commented:
I can't do that.I can't use 4 regular expression  validators for Business phone number.Then 3 to 4 error messages will be displayed and the  alignemnt will not be good.
The 4 text boxes are in single line.
Is there any other option instead of using 4 regular expression validators.

Thanks.
0
 
käµfm³d 👽Commented:
I forgot about this earlier, but you could put a CustomValidator on your page, add a handler to the OnServerValidate event, and then evaluate the pattern on the concatenation of the four TextBoxes.

Here is an example:

Markup
<asp:TextBox ID="areaCode" runat="server" />
<asp:TextBox ID="exchange" runat="server" />
<asp:TextBox ID="subscriber" runat="server" />
<asp:TextBox ID="extension" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" OnServerValidate="Validate_Function" ErrorMessage="Not good!" />
<asp:Button ID="Button1" runat="server" Text="Button" />

Open in new window


Code-Behind
protected void Validate_Function(object sender, ServerValidateEventArgs e)
{
    string src = string.Concat(this.areaCode.Text, this.exchange.Text, this.subscriber.Text, this.extension.Text);

    e.IsValid = System.Text.RegularExpressions.Regex.IsMatch(src, @"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$");
}

Open in new window

0
 
KavyaVSAuthor Commented:
I am already using custom validator. I am concatenating the numbers entered in four text boxes and comparing against the regular expression.It is giving single error message for 4 textboxes.

The problem came becoz extension is optional.When I entered 2 digits in areacode,3 digits in 2nd text box,2 digits in 3rd TextBox and 3 digits in 4th Text Box it is returning true.
Same thing when I entered only one digit in area code TextBox and remaining 2 digits in 4th text box becoz the total no.digits equal to 10.

How to overcome this.The user has to enter 3 digits in 1st and 2nd Text Boxes and 4 digits in 3rd Text Box.

Thanks.
0
 
käµfm³d 👽Commented:
>>  The problem came becoz extension is optional

The post above (http:#35192466) takes this into account.
0
 
KavyaVSAuthor Commented:
>>  The problem came becoz extension is optional

(http:#35192466) doesn't  takes this into account.

The user has to enter 3 digits in 1st and 2nd Text Boxes and 4 digits in 3rd Text Box.Without doing that,if the total number of digits in 4 textboxes equal to 10 it is returning true.

I am hoping I am conveying it correctly.

Thanks.

0
 
käµfm³d 👽Commented:
Ah. I think I understand now. And you said that setting MaxLength didn't work? Strange. We can overcome that with a bit of code:
protected void Validate_Function(object sender, ServerValidateEventArgs e)
{
    if (this.areaCode.Text.Length != 3 && this.areaCode.Text.Length != 5 && this.exchange.Text.Length != 3 && this.subscriber.Text.Length != 4)
    {
        e.IsValid = false;
        return;
    }

    string src = string.Concat(this.areaCode.Text, this.exchange.Text, this.subscriber.Text, this.extension.Text);

    e.IsValid = System.Text.RegularExpressions.Regex.IsMatch(src, @"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$");
}

Open in new window

0
 
käµfm³d 👽Commented:
Correction:
protected void Validate_Function(object sender, ServerValidateEventArgs e)
{
    if ((this.areaCode.Text.Length != 3 && this.areaCode.Text.Length != 5) || this.exchange.Text.Length != 3 || this.subscriber.Text.Length != 4)
    {
        e.IsValid = false;
        return;
    }

    string src = string.Concat(this.areaCode.Text, this.exchange.Text, this.subscriber.Text, this.extension.Text);

    e.IsValid = System.Text.RegularExpressions.Regex.IsMatch(src, @"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$");
}

Open in new window

0
 
KavyaVSAuthor Commented:
I tried this way.

Protected Sub Validate_Form(ByVal sender As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
    string src = areaCode.Text+exchange.Text+subscriber.Text+extension.Text
    string phno =areaCode.Text+exchange.Text+subscriber.Text
    e.IsValid = System.Text.RegularExpressions.Regex.IsMatch(src, @"^\(?([2-9][0-8][0-9](?<!11))\)?([2-9][0-9]{2})?([0-9]{4})([0-9]{0,4})$"
If e.IsValid AndAlso Not phno.Trim().Length.Equals(10) Then
                            CustomValidator1.Text = "Enter Business Phone"
                            args.IsValid = False
End If
 End Sub


Thanks for all your prompt responses and your help.It really helped me. I will give A grade.

0
 
KavyaVSAuthor Commented:
One thing I forgot to mention, maxlenght propery for TextBox i s working.Maxlength means it only upper limit right.I set the maxlength for TextBox as 3.so the user can enter upto 3 digits not 4 digits,but he can enter only 2 digits also.There is no minimum length property.If it is there then I don't need to use above code.

Thanks.
0
 
käµfm³d 👽Commented:
>>  I tried this way.

You can certainly do that, but I don't see how it overcomes what you described in http:#35198517 .

MaxLength should mean, "accept, at most, this many characters." I don't believe there is a MinLength property.
0
 
KavyaVSAuthor Commented:
It overcomes what I  described in http:#35198517 .
If validation returns true(after comparing the string in 4 textboxes against regular expression),then I will check whether the string length in first 3 text boxes equal to 10,if it is not then the error message will be displayed.
By doing this way,I am able to use single custom validator for 4 text boxes.


Yes,there is no minlenght property.

Thanks.
0
 
käµfm³d 👽Commented:
>>  By doing this way,I am able to use single custom validator for 4 text boxes.

If it works, then I can't complain  = )
0
 
KavyaVSAuthor Commented:
Thanks for all your prompt,useful answers and your precious time.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 13
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now