Allow certain special characters - Regular expression - asp.net

I want to allow only spaces, hyphens, underscores & commas along with alphabets & numbers. But this is not working as desired.
<asp:RegularExpressionValidator ControlToValidate="txtQuestion"
Display="dynamic" runat="server" ErrorMessage="*"
ValidationExpression="^[a-zA-Z0-9-_,\s]$"></asp:RegularExpressionValidator>
Character ShownResult
- valid
_ valid
, valid
-_ invalid //this should be valid for me

Put - symbol at the last or at the first inside the character class or consider escaping it otherwise it would act as a range operator.
^[a-zA-Z0-9_,\s-]+$
Add + after the character class to allow one or more characters otherwise it would allow only a single character from the list.

^[a-zA-Z0-9\-_,\s]*$
Escape - so that it does not define a range.Also use *or + to include one or more characters.
See demo.
http://regex101.com/r/hQ1rP0/21

If you want text field to except only Numbers and Char #%?* use following RE
ValidationExpression="[0-9#%?]"
This will work for any combination of characters

Related

Regular Expression validation in ASP.NET for special chars

I need to validate the scenario in Regex, I'm using RegularExpression validation in ASP.NET.
Shouldn't start or end with SPACE
Doesn't contain only SPACE
whole string shouldn't contain two special char "#" & "?"
Valid:
"as#d qwe2", "&^%$$(&+_", "12#$.p"
InValid:
" ", "asd ", " asd#", "ksdhf?kh", "asdf#asd"
I'm trying with this:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="RegularExpressionValidator"
ValidationExpression="^[^\s]+(\s+[^#?]+)*[^\s]$">Error</asp:RegularExpressionValidator>
Untested RegEx: ^([^ #?][^#?]*[^ #?]|[^ #?])$
There are a couple of problems with the regex you are currently using:
^[^\s]+ is matching almost all of the string if it does not start with a space character (thus matching # or ?).
Due to the way the regex is constructed, you can only input strings of length 2 and above. This is a minor setback, but can be avoided.
I would suggest using negative lookaheads since there are multiple 'checks' to do on the first character:
^(?!\s|.*\s$)[^?#]+$
(?!\s|.*\s$) will prevent a match if the string begins with \s or ends with \s and [^?#]+ matches all characters but ? and #. Space only strings will automatically be rejected because space only strings will have to begin with a space.

RegularExpressionValidator fails if more than one character is entered

This my asp:RegularExpressionValidator
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
runat="server" ControlToValidate="uxTrachoCtrl1"
ErrorMessage="Ne dois pas contenir des caractères alphabétiques"
ValidationExpression="[0123456789,.<>=]" ValidationGroup="verification" Display="Dynamic"
SetFocusOnError="True">
</asp:RegularExpressionValidator>
The string can contain only those characters 0123456789,.<>=
This my regex [0123456789,.<,>,=]
It works if I type one character like f or 1, but if I put more than one character this will raise an error:
ex: input="1"=ok
input="f"=error
input="11"=error (It's supposed to be right)
The character class matches only one character. You need to repeat it if you want to allow arbitrary length characters:
"[0-9,.<>=]*"
If you want to exclude empty inputs use this instead:
"[0-9,.<>=]+"
Note that my character class is equivalent to yours (0-9 is a shorthand notation for 0123456789 and you had the , multiple times in your character class).
you just have defined the range of valid characters for one character
change it to
ValidationExpression="[0-9,.<>=]{minLength,maxlength}"
instead of minLength and maxLength you should put your desired numbers .
or use *|+ if you want to allow 0|1 or more repeat of characters as others suggested

Regular expression, required field only with alphanumeric

So how do I add that it should not have empty field in this same line including my other validation for alphanumeric reg ex.
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
I tried this and didn't work
^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$ | /\S/
This is for validation controls
<asp:RegularExpressionValidator id="userLocationValidation" runat="server"
ControlToValidate="userLocation"
ValidationExpression="/\S/"
ErrorMessage="Only use letters from the english alphabet a-z">
</asp:RegularExpressionValidator>
Use:
^[A-Za-z0-9 _]+[A-Za-z0-9][A-Za-z0-9 _]*$
The '+' says one or more, so empty fields will fail.
^[a-zA-Z0-9 _]+$
+ means at least one
if you mean at least one not white-space character something like
^[a-zA-Z0-9 _]*[a-zA-Z0-9_]+[a-zA-Z0-9 _]*$

Why do greater than and less than symbols match in the following regex?

I'm trying to limit the punctuation that a user can enter into a text box and am using this regex:
^[\w ,-–\[\\\^\$\.\|\?\*\+\(\)\{\}/!##&\`\.'\n\r\f\t""’]*$
Why do > and < produce a match? They are not included in the regex.
NOTE: this is being used in a asp.net regular expression validator.
Edit: here's the asp.net source:
<input runat="server" type="text" id="txt_FName" class="textbox" maxlength="60" />
<asp:RegularExpressionValidator ID="rfvRegexFName" runat="server" ControlToValidate="txt_FName" ErrorMessage="<%$ Resources:Subscribe, inputValidationError %>" />
In the code behind I add the expression:
rfvRegexFName.ValidationExpression = #"^[\w ,-–\[\\\^\$\.\|\?\*\+\(\)\{\}/!##&\`\.'\n\r\f\t""’]*$";
Why do > and < produce a match?
Probably because the - (hyphen) in ,-– matches the character range [, to –]. Either escape the hyphen: ,\-– or place the hyphen at the very start or end of the class which causes it to match the literal - instead.
Also note that you need not escape the $, ., |, ?, *, +, (, ), { and } inside a character class
Edit: After seeing the other answers, it looks like there might have been a few things going on here. The main problem was the unescaped dash, though. For future reference of anyone reading this Q/A thread, see Bart Kiers' answer.
You don't want to escape the period. When it's inside the brackets, it matches a regular period by default, not any character like it does normally. I'm not positive, but that might be making it act as a special character again, therefore matching anything.
Try this:
^[\w ,-–\[\\\^\$.\|\?\*\+\(\)\{\}/!##&\`'\n\r\f\t""’]*$
Try changing the last * to a +. You're matching zero or more instances, which always guarantees a match.
Edit to add: Are all of those characters regular ASCII? It looks like you might be using an em-dash or something, which might be related to your problem.

Regular Expression to test for the number of any characters in a range

I'm using the ASP Validation control a wanted to test that multiline textbox content was between 2 and 40 characters. Any character can be provided, including the new line character. What would be the expression? What I have below fails.
<asp:RegularExpressionValidator ID="RegularExpressionValidator"
runat="server"
ValidationExpression="^[.]{2,40}$"
ControlToValidate="CommentsTextBox"
ErrorMessage="The Comment field must be between 2 and 40 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
When you put the dot inside square brackets, it loses its special meaning and just matches a literal dot. The easiest way to match any character in an ASP RegularExpressionValidator is like this:
^[\s\S]{2,40}$
[\s\S] is the standard JavaScript idiom for matching anything including newlines (because JS doesn't support DOTALL or s-mode, which allows the dot to match newlines). It works the same in .NET, so you don't have to worry about whether the dot matches newlines, or whether the regex will be applied on the server or the client.
You're treating the period as a literal inside the brackets. You just want:
^(.|\n){2,40}$

Resources