RegularExpressionValidator fails if more than one character is entered - asp.net

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

Related

RegularExpressionValidator is not working in ASP.NET

My RegularExpressValidator1 does not fire. Not sure what is missing below. Any suggestion?
<td class="txtbox1"><asp:TextBox ID="txtCard" runat="server" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtCard"
Forecolor="Red"
ValidationExpression="(0*[1-9]\d*)" runat="server"
ErrorMessage = "Enter numeric number only.">
</asp:RegularExpressionValidator>
</td>
Take a closer look at Microsoft's page How To: Use Regular Expressions to constrain input in ASP.Net.
Enclosing the expression in the caret (^) and dollar sign ($)markers ensures that the expression consists of the desired content and nothing else. A ^ matches the position at the beginning of the input string and a $ matches the position at the end of the input string. If you omit these markers, an attacker could affix malicious input to the beginning or end of valid content and bypass your filter.
If you scroll down to the section titled Common Regular Expressions, it includes a RegEx for positive integers:
^\d+$
Their pattern restricts the input to only numbers, whereas yours seems like it's only checking to see if a number is included in any fashion.
As an example, consider this input:
1234 and text!
Using ^\d+$ (Source)
0 matches
Using (0*[1-9]\d*) (Source)
1 match: 1234
As an unrelated note, I'd suggest modifying your placeholder text from "Enter a numeric number only." to something like "Enter a number" or "Enter a numeric value", as "numeric number" is a bit redundant!

Allow certain special characters - Regular expression

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

regular expression to check a symbol

I have a text box to input.
<asp:TextBox ID="txt_namepair" runat="server" Text="<Name>=<Value>">
</asp:TextBox>
I want to add validation using regular expression validator. Now I need regular expression to check the input format as
"alpha numeric string = alphanumeric string"
like <name>=<value> pairs.
When I click on add button those = pairs will be displayed in a listbox.But what I wanted is, I want to check whether those , pairs have equal sign(=) between them by using regular expression. And and pairs should be alpha numeric characters.
You can use the following regular expression
^[a-zA-Z0-9 ]*=[a-zA-Z0-9 ]*$
to match your string
"alpha numeric string = alphanumeric string"
P.S. Do you really need spaces? If not delete the space from the regular expression after 9.
You can test the code I have provided at the following site
http://regexhero.net/tester/

Code for Alpha Numeric used in Regular Expression in asp.net

I need code for alpha numeric ,which is used in regular expression in asp.net to enter the data inside the textbox .
Regex for alpha numeric:
"^[a-zA-Z0-9]*$"
If you don't want empty strings, use + instead of *.
I guess below should be enough.
^[0-9A-Za-z]*$
Here is the break down
^ indicates start of the string
0-9 all number from 0 to 9
A-Z all Upper case character
a-z all small case character
* indicates 0 or more character
$ end of string
Use this ValidationExpression="^[a-zA-Z0-9.#]{0,25}$". It accepts . and # symbol also. If not remove them
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ValidationExpression="^[a-zA-Z0-9.#]{0,25}$" ErrorMessage="Only Alphanumeric"></asp:RegularExpressionValidator>

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 _]*$

Resources