Regular Expression Validator - Illegal characters

kristoffera

New member
Joined
May 10, 2005
Messages
3
Is there any way to reverse the "Regular Expression Validator" to check for illegal characters. For example: If a string contains any of a,b or c I want the RegExp Validator to generate an error. If it contains any other characters I want it to pass. Any help would be much appreciated!

// Kristoffer
 
Sure - you just have to construct your regular expression properly.

You might refer to "regular expressions, syntax" in MSDN for a list of all the characters and symbols one can use in a regular expressions.

For ex:

[^a-c]

matches any character thats not a through c, but chances are youll need more than just that to accomplish whatever youre after.

Paul
 
PWNettle said:
Sure - you just have to construct your regular expression properly.

You might refer to "regular expressions, syntax" in MSDN for a list of all the characters and symbols one can use in a regular expressions.

For ex:

[^a-c]

matches any character thats not a through c, but chances are youll need more than just that to accomplish whatever youre after.

Paul

Thanks for the help Paul! My RegExp now looks like this.

/^[^\"\]*$/

All i need now is to match an empty string as well. How do I do that.
The goal is that it should not let my users enter nothing or any of " or .
Just started using RegExp so please feel free to stat the obvious!

Cheers // Kristoffer
 
it might be easier if you reverse your logic to
-> match strings that contain quotes or all whitespace
(^([\"\]+)$)|(^([\s]+)$)
 
kristoffera said:
Thanks for the help Paul! My RegExp now looks like this.

/^[^\"\]*$/

All i need now is to match an empty string as well. How do I do that.
The goal is that it should not let my users enter nothing or any of " or .
Just started using RegExp so please feel free to stat the obvious!

Cheers // Kristoffer

Validator controls seem to not fire at all if the target control is empty. So, sadly, if you want to validate by regular expression and want the field to be required (no empty strings) then you may need to use two validators - a regular expression validator and a required field validator.

I think custom validators always fire - so you can duplicate the functionality of both in one custom validator - but to me this seems like a poor solution (as in, it seems like one shouldnt have to do this).

If someone knows a way around this Id be glad to hear/read it because using two validators in this type of situation irks me!

Paul
 
HJB417 said:
it might be easier if you reverse your logic to
-> match strings that contain quotes or all whitespace
(^([\"\]+)$)|(^([\s]+)$)

Thanks again for the help... However, I didnt have to reverse or add the \s.
I got it working perfectly in JavaScript anyway, but as Paul wrote the Regular Expression Validator doesnt seem to fire if the field is empty.
I wouldnt mind knowing how to do it all without having to use 2 validators.

BTW. Heres what I ended up with: /^[^\\"][^\\"]*$/

Best // Kristoffer
 
Back
Top