Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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 that's not a through c, but chances are you'll need more than just that to accomplish whatever you're after.

 

Paul

Posted
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 that's not a through c, but chances are you'll need more than just that to accomplish whatever you're 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

Posted
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 shouldn't have to do this).

 

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

 

Paul

Posted
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 didn't have to reverse or add the \s.

I got it working perfectly in JavaScript anyway, but as Paul wrote the Regular Expression Validator doesn't seem to fire if the field is empty.

I wouldn't mind knowing how to do it all without having to use 2 validators.

 

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

 

Best // Kristoffer

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...