Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case-Insensitive RegularExpressionValidator

Status
Not open for further replies.

melimae

ISP
Jul 29, 2005
16
US
I am trying to use a regularexpressionvalidator to verify that a valid state abbreviation was entered. I want it to be case insensitive. Below is my control, but it is not working correctly. I get a syntax error.

<asp:RegularExpressionValidator ControlToValidate="txtBankState" ID="valStateLen" runat="server" ErrorMessage="Please enter a valid state." ValidationExpression="(?i:A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])" text="*"/>

I have also tried
ValidationExpression="(?i)(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])"

and ValidationExpression="(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])"

The last one works, but only for capital letters.

I have also tried using style="text-transform:uppercase" on my text box control but the regularexpressionvalidator does not work when I use it.

Any help is greatly appreciated.
 
If you've got RegEx problems, is a great resource for finding out they different syntax that you can use.

Also, try looking at Listing 3.9 in this article:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
If you want to use the built-in RegularExpressionValidator, you'll have to accomodate for case in the expression.

For instance, instead of:

^(A[LKSZRAEP]|C[AOT])$

You'd have to do something like:

^([Aa][LlKkSsZzRrAaEePp]|[Cc][AaOoTt])$

You have two alternatives, however, which are:

1. Implement your own validation via a CustomValidator control where you use RegexOptions.IgnoreCase on the server validation, and /i in the ClientValidationFunction.

2. Better yet, use a DropDownList with all the possible state abbreviations. You help guard against junk data in that case and it's more usable besides.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top