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>
Related
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>
I have the following TextBox and RegularExpressionValidator
<asp:TextBox ID="txtQuantity" runat="server" MaxLength="7"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ErrorMessage="Enter only positive integer values."
ControlToValidate="txtQuantity" ValidationExpression="^[1-9][0-9]*$"
CssClass="required" Display="Dynamic" />
It is reading all the positive integers properly and giving error messages on wrong entries.
But validation is getting failed when a positive integer preceding with a 0 is entered.
Example: "098", "09" etc
Should I change my regular expression or the logic?
You can allow zeros with non-zeros and disallow just zeros (or empty string) with
^(?!0+$)[0-9]+$
See demo
REGEX EXPLANATION:
^ - Start of string (not sure it is necessary, I think the regex is anchored by default, but please check)
(?!0+$) - A negative lookahead that checks if the whole string is not equal to 0 (or 0000)
[0-9]+ - 1 or more digits from 0 to 9 (note that it already does not allow empty string)
$ - End of string (same note as for ^)
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
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
Can anyone give me an example of a regular expression for must be loger than {n} and must include at least 2 digit integer
I have the following for now but it only validates the length;
<asp:RegularExpressionValidator ID="myTxtVal"
runat="server" ControlToValidate="myTxt"
ErrorMessage="Input Is Too Short" ValidationExpression=".{15}.*" />
<asp:RegularExpressionValidator ID="myTxtVal"
runat="server" ControlToValidate="myTxt"
ErrorMessage="Input Is Too Short" ValidationExpression="^(?=.*?\d{2}).{15}" />
Edit: fixed for 2 digit integer. Not the same as 'has 2 digits'... ;-)
It uses the lookahead to validate that there are at least two digits in a row, then the .{15} part to match 15 characters. It doesn't need to match the rest of the string, so I removed the .*.
Something like that?
^(?=.*\d{2}).{4,}$
See it here at Regexr
The first construct (?=.*\d{2}) is a look ahead, it checks if somewhere in your string are 2 digits in a row. (I am not sure at this point if it is what you need)
The second part checks .{4,} checks the length of the string for at least 4 characters.
^ anchors the pattern to the start of the string
$ anchors the pattern to the end of the string
Just another method, purely for your interest:
/^(.|()\d\d){N-1}\2/
This works in flavours of regex where back referencing with \N fails to match if the Nth group to which it refers failed to match. The expression can only successfully match if the second group was matched, which in turn can only happen if \d\d matches. For example, N = 15:
/^(.|()\d\d){14}\2/