In asp.net, I want to use regularexpressionvalidator for phone number. requirement is only to allow numbers and Dash (-)
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhone" ErrorMessage="Not a Valid Phone Number" ValidationExpression="\d*">
The above code only validates numbers but not Dash -. Like user randomly put
23333-34 (should accept) or any combination. It is not must that - will be there. It can be numbers only some times.
You could use this as the ValidationExpression:
[0-9-]*
to accept any number of digits and dashes (including "---").
You could also use this:
([0-9]\-?)*
to accept digits, optionally with single dashes inbetween (or after). Use {5,10} instead of * to accept a minimum of 5 and a maximum of 10 digits (adjust the numbers to your needs).
For more regex info, see MSDN.
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="reg" runat="server" ValidationGroup="VGrp"
SetFocusOnError="true" ErrorMessage="Phone is invalid"
ControlToValidate="txtPhone" ValidationExpression="\d*"></asp:RegularExpressionValidator>
try this.
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'm uaing following regular expression for validating decimal or numeric (18,3) numbers,
it does disallow users from entering more than 1 decimal points and all that
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid" ControlToValidate="txtqty" ValidationExpression="^[-+]?[0-9]*\.?[0-9]*([?[0-9]+)?$" ValidationGroup="save"></asp:RegularExpressionValidator>
but i also want to disallow users from entering any value equal to 0 like the following eg:
0.00,
0000.00,
00.0,
0...
I think the problem is that you are using one validator to perform more than one task
Divide your problem:
Validate the format
Validate specific values
Use a simple CompareValidator in addition to your RegularExpressionValidator
<asp:CompareValidator ErrorMessage="Value must be grater than 0" ControlToValidate="TextBox1"
runat="server" Operator="NotEqual" Type="Double" ValueToCompare="0" />
"Use following regular expression:
^\d[1-9]*(\.\d+)|([0-9])$
it disallow values in following pattern:
00.00,0123.78 i.e value before decimal followed by 0
You can used this
^([1-9][0-9]*\.[0-9]+)|([0-9]+)$
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Invalid" ControlToValidate="txtqty" MaximumValue="99999" MinimumValue="1.0000" ValidationGroup="save"></asp:RangeValidator>
range validator works
I'm currently coding a ASP.NET web application using VB.NET. I'd like to know how to limit the input of the regular expression validator to numbers only (with specific number of digits e.g 7-20 digits).
You can use curly brackets in regular expression to limit like this
^[0-9]{7,20}$
This will limit it from 7-20 digits. For detail explanation see this link
http://msdn.microsoft.com/en-us/library/ms972966.aspx
<asp:RegularExpressionValidator ID="req" runat="server" ErrorMessage="hi"
ControlToValidate="txt1" ValidationExpression="^[0-9]{7,20}$">
</asp:RegularExpressionValidator>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
This is an another way to allow numeric only with minimum 7 and maximum 20 characters
Use this
ValidationExpression="^[0-9]{7,20}$"
If I have an IP address: 192.100.100.2 and need to ensure that it falls within a range specified using wildcard patterns.
The patterns can be either:
1. 192. *. *. *
2. *. *. *. *
3. 192.1**. *.2
So essentially, an asterisk or three asterisks specify the valid range. Is there something built in in ASP.NET I can use to validate the IP address or would this be more of a custom validation?
As #AtoMerZ said, just convert your patterns to regular expressions:
''//Patterns to search for
Dim Patterns() As String = {"192.*.*.*", "*.*.*.*", "192.1**.*.2"}
''//Test IP
Dim TestIP = "192.100.100.2"
''//Loop through each pattern
For Each P In Patterns
''//Swap two asterisk for two regex digits (\d\d) and one asterisk for one or more digits. Also escape the period
Trace.WriteLine(System.Text.RegularExpressions.Regex.IsMatch(TestIP, P.Replace("**", "\d\d").Replace("*", "\d+").Replace(".", "\.")))
Next
Convert it to string and use Regex.Match
Use this validation expression
ValidationExpression="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
example code
<asp:TextBox ID="TextBox1" runat="server" Width="321px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Validate" />
<br />
<asp:RegularExpressionValidator ValidationExpression="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
ID="RegularExpressionValidator1" runat="server" ErrorMessage="Invalid IP !" ControlToValidate="TextBox1"></asp:RegularExpressionValidator></div>
I have the following asp.net markup:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"
ValidationGroup="passwordValidation"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
ControlToValidate="txtPassword" Text="Required" ValidationGroup="passwordValidation" />
<asp:RegularExpressionValidator runat="server" ControlToValidate="txtPassword"
Text="Passwords should contain a minimum of 7 characters with at least one numeric
character." ValidationExpression="^(?=.*\d{1})(?=.*[a-zA-Z]{2}).{7,}$"
ValidationGroup="passwordValidation" Display="Dynamic"></asp:RegularExpressionValidator>
If I type in a password like test1234, it passes in chrome and firefox, but the message that my password should contain a minimum of 7 characters with at least one numeric character is shown in internet explorer
You're probably getting bitten by the infamous IE regex lookahead bug. You should be able to work around that by making the length check a lookahead like the other conditions, and putting it first.
^(?=.{7,}$)(?=.*\d)(?=.*[a-zA-Z]{2}).*
But I think I see another problem. (?=.*[a-zA-Z]{2}) matches two consecutive letters; is that really your intent? If you want to require at least two letters, but not necessarily consecutive, you should use (?=.*[a-zA-Z].*[a-zA-Z]).