RegularExpressionValidator is not working in ASP.NET - 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!

Related

RegularExpressionValidator to not contain any spaces as well as any of the following characters `~!##$%^&*()=+[]{}\|;:'",<>/?_ with specific length

I am trying to implement this rule with regex : "The name at asp:textbox that is given as input by a user must not contain any spaces as well as any of the following characters:"
`~!##$%^&*()=+[]{}\|;:'",<>/?_ .
And also the length must not be greater than 15.
I am using something like this:
<asp:RegularExpressionValidator ID="validateName" runat="server" Text="*" ValidationExpression="(?=^.{1,15}$)(^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$)" ControlToValidate="txtName" Display="Dynamic" SetFocusOnError="true" ValidationGroup="Wizard"> </asp:RegularExpressionValidator>
BUT the problem is that test12. is not valid (right) and test12.test.something is passing, which is not right for my issue.
Also I tried this regex without result as asdasdads.gr passes (not right):
(?=^[A-Za-z0-9\-]{1,15}$)
Maybe I made it too complicated and now I am getting stuck.
Since your requirements are:
No characters from this set: `~!##$%^&*()=+[]{}\|;:'",<>/?_ .
And also the length must not be greater than 15.
You may just use a negated character class with {1,15} limiting quantifier:
ValidationExpression="^[^\][^`~!##$%^&*()=+{}\\|;:'",<>/?_\s.]{1,15}$"
See regex demo. We must escape the \ symbol and we need to escape the ] symbol (as it can be used by JS engine). Since the \ is used to specify escape sequences, escape it, too (use double slashes to denote one literal \). Note you need to serialzie the double quote as " (or use a hex representation for a regex - \x22) if you are using it inside some HTML attribute.
Detailed explanation:
^ - start of string
[^\][^`~!##$%^&*()=+{}\\|;:'",<>/?_\s.]{1,15} - 1 to 15 characters (due to the limiting quantifier {1,15}) that are not in the defined set (a [^...] is a negated character class). If you plan to allow empty string, use {0,15} quantifier.
$ - end of string
In the comments we discussed trying only searching for valid characters.
[A-Za-z0-9\-]{1,15}
This should work for "test12" if you need "test12." just add the full stop in there. Also \d is for digits instead of using 0-9
[A-Za-z\d\-\.]{1,15}
if you want to allow it to have an optional full top at the end use the following regex. The ? means 0 or 1.
[A-Za-z\d\-]{1,15}\.?
Example Here
Finaly this worked for me:
ValidationExpression="^[^\][^`~!##$%^&*()=+{}\\|;:'\x22,<>/?_\s.]{1,15}$"
Full code:
<asp:TextBox ID="txtHostname" runat="server" CssClass="NormalTextBox" Text=""></asp:TextBox>
<asp:RequiredFieldValidator ID="HostnameValidator" runat="server" Text="*" Display="Dynamic"
ControlToValidate="txtHostname" meta:resourcekey="HostnameValidator" SetFocusOnError="true"
ValidationGroup="VpsWizard">
*
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="valCorrectHostname" runat="server" Text="*" meta:resourcekey="valCorrectHostname"
ValidationExpression="^[^\][^`~!##$%^&*()=+{}\\|;:'\x22,<>/?_\s.]{1,15}$"
ControlToValidate="txtHostname" Display="Dynamic" SetFocusOnError="true" ValidationGroup="VpsWizard">
</asp:RegularExpressionValidator>

Make MaskedEditExtender Mask optional

Here I have a MaskedEditExtender, with a validator using a regex.
It validates phone numbers with 8 or 9 digits:
<asp:TextBox Style="width: 135px" ID="txtTelefone" runat="server"></asp:TextBox>
<ajaxToolkit:MaskedEditExtender
ID="MaskedEditExtender_Telefone"
TargetControlID="txtTelefone"
runat="server"
Mask="\(99\)9999NN9999"
OnInvalidCssClass="txt-TextBox-Error"
ValidateRequestMode="Enabled"
ErrorTooltipEnabled="True"
Filtered="-"
PromptCharacter=" "
ClearMaskOnLostFocus="false"/>
<ajaxToolkit:MaskedEditValidator
ID="MaskedEditValidator_Telefone"
runat="server"
ControlExtender="MaskedEditExtender_Telefone"
ControlToValidate="txtTelefone"
ValidationExpression="^\(\d\d\)\d\d\d\d+-\d\d\d\d$"
Display="Dynamic"></ajaxToolkit:MaskedEditValidator>
The issue is: as you can see in the regex, the user can put 4 or 5 digits between the ')' and the '-'.
But the "Mask" field doesn't allow it.
I need the MaskedEditExtender to stop crying when I don't type all the characters, because they're not necessary. All I need to validate my field is the regex.
The MaskedEditExtender is there only to give a mask that allows me to type only numbers and have a (99) in the beggining. It does not need to validate anything.
Well, seems like there's no such functionality in MaskedEditExtender that allows you to put less than the characters in the mask, so I did a small workaround:
I've put autocomplete in the mask, adapted the regex to accept an empty space in the end of the string and trimmed it everytime I wanted to use the TextBox's value.
In MaskedEditValidator:ValidationExpression="^\(\d{2}\)\d{4,5}-\d{4} *$"
In MaskedEditExtender:AutoComplete="true" AutoCompleteValue=""
In CodeBehind: txtTelefone.Text.Trim();
With this, all unfilled characters will be replaced as space in the end of the string, the regex will take care of the validation, and the Trim() will remove the spaces. Thus allowing you to make the Mask's length optional.
Set ClearMaskOnLostFocus="true" on MaskedEditExtender.

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/

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

Trouble with the simplest regex

I'm trying to validate an ASP TextBox that can only have 1-2 digits (not zero) using the following regex:
^[1-9]{1,2}
This regex doesn't validate if the field is empty (assumed it would due to the 1-2 part)
Also tried ^[1-9+]{1,2} to no avail.
These are my controls:
<asp:TextBox ID="txtInHour"
MaxLength="2"
runat="server"
Text='<%# Bind("InHour") %>'
Width="80"/>
<asp:RegularExpressionValidator ID="rvInHour"
ControlToValidate="txtInHour"
Display="None"
ValidationExpression="^[1-9]{0,2}$"
runat="server"
ErrorMessage="InHour is incorrectly formatted." />
The first thing I notice is that you don't allow zeros in your pattern. So 10 or 20 is not valid? The second thing is that you start with "starts with" AKA "^" but there's no "ends with" AKA "$"
So.. try this:
^[1-9][0-9]?$
In human readable:
starts with 1-9, followed by an optional digit from 0-9, end of string.
On the other hand I don't know what you've meant with ("no zeros") - no zeros at all?!
I found out that for some reason RegularExpressionValidators don't work when there's no input to match against (blank fields) so I had to use a seperate RequiredFieldValidator. Thanks for your input everyone!
You can use this regex :
^([1-9]|[1-9][0-9])$

Resources