tags:

views:

107

answers:

1

I want to prevent the entry of two characters together but I want the user to be able to enter one or the other as well as use the enter key. I would like to use a white list instead of black listing characters. The regular expression also needs to support a min and max length. I'm doing client side validation using the asp:regularexpression control. I do not want to have to do server side validation unless that's the only solution to this problem. Has anyone else run into this? Thanks in advance for any help.

Here is the regex I'm currently using:

(?!.(&#))^[a-zA-Z0-9!@#$%^&_=+~''"";:, \r\n.()\?-]{1,1000}$

I'm using a asp.net textbox that is set to multiline so I have to allow for \r and \n or what's the point of using a multiline textbox :)

I want to keep the user from entering &# together but allow them to enter text with & or # in it and allow all of the characters a-zA-Z0-9!@#$%^&*_=+~''"";:, \r\n.()\?-.

A valid text entry would be as follows:

I have a question about my order. The order number is 12345. Can you help me?

An invalid text entry would be as follows:

I am trying to keep the user from entering &# in the textbox, but I want to allow them to enter & or #.

A: 

string pattern = @"^(?!.*&#).{1,1000}$"

This asserts that the string does not contain the combination &# and otherwise allows anything between 1 and 1000 characters long, inclusive. If you really want the whitelist, replace the . preceding the { with your whitelist character class (inside []).

Jay
close but still not working - does prevent but doesn't allow for return key and since this is a multiline textbox that's key
Delana Baker
The pattern is correct as is. You have to specify `RegexOptions.Singleline`.
Jay
RegExOptions is not available for the regular expression control. We're not doing server side validation.
Delana Baker
See if this works (I haven't tested it) : `^(?!(?:.|\s)*)(?:.|\s){1,1000}$`
Jay