-1

I am supposed to use the regularExpressionValidator to verify a ZIP code for a basic webpage I'm making. If the Zip code is valid, the submit button's click event procedure should display the message "Your ZIP code is" followed by the ZIP code and a period.

I don't know how to do an "if" statement to check to see if the zip is valid or not

**Why does the value = 0 when I enter 60611-3456

2 Answers2

2

...don't know how to do an "if" statement...

You were assigned to use a RegularExpressionValidator, and this sounds like homework. If so, it also sounds like the purpose of the assignment is to make this happen without writing any if statements at all.

The validator controls have a feature where a postback event will not occur if validation fails. You use a correct regular expression with a correctly configured validator control, and the code that shows the "Your zip code is..." message will never run. Configuring the validator control is the point of the assignment; you need to do that part on your own. But finding an acceptable regular expression is a distraction from the real learning, and so I don't mind just giving that to you:

^\d{5}(-\d{4})?$

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • `\d{5}` will also match strings like `123456` or `a12345bcd`. Switching it to `^\d{5}$` should take care of that. – valverij Dec 02 '13 at 21:38
  • This will not handle 9 digit zip codes like the OP's example: 60611-3456 – Chris Dunaway Dec 03 '13 at 16:19
  • @ChrisDunaway That edit was added after my post. I suspect 9 digit zip won't matter for the homework problem, as I believe this is more about just setting up the validation control correctly, but since you brought it up I can fix the expression. – Joel Coehoorn Dec 03 '13 at 16:24
0

The issue is that your regular expression indicates the four digits must exist if you have the dash. Generally that would be okay but since you're using an input mask the dash always exists, even when it's only five digits. Try the following expression.

ValidationExpression="\d{5}-?(\d{4})?$"

Hope it helps.

Ruban J
  • 622
  • 1
  • 7
  • 31