Solved

Validating ZipCode,Phone Number and Address

Posted on 2011-03-21
25
323 Views
Last Modified: 2012-05-11
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
Comment
Question by:KavyaVS
  • 13
  • 12
25 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35181573
>>  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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35181601
P.S.

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

Open in new window

0
 

Author Comment

by:KavyaVS
ID: 35182104
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35182385
>>  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
 

Author Comment

by:KavyaVS
ID: 35182871
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35182974
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
 

Author Comment

by:KavyaVS
ID: 35183541
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35183799

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

Open in new window

0
 

Author Comment

by:KavyaVS
ID: 35190642
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35190809

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

Open in new window

0
 

Author Comment

by:KavyaVS
ID: 35191370
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35192029
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:KavyaVS
ID: 35192124
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35192466
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
 

Author Comment

by:KavyaVS
ID: 35198200
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35198250
>>  The problem came becoz extension is optional

The post above (http:#35192466) takes this into account.
0
 

Author Comment

by:KavyaVS
ID: 35198517
>>  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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35199141
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35199161
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
 

Author Comment

by:KavyaVS
ID: 35200705
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
 

Author Comment

by:KavyaVS
ID: 35200751
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35200977
>>  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
 

Author Comment

by:KavyaVS
ID: 35206292
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35206474
>>  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
 

Author Comment

by:KavyaVS
ID: 35215137
Thanks for all your prompt,useful answers and your precious time.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now