Conditional Validation on a text field - c#

I have a text field and I want to have a validation which verify if it starts with 0 the length will be 10, else will be 13.
I want to know if I can write in validation expression like "^0..." not in function.
I hope you can help me.
I code in ASP.NET, C#.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="dsg" ErrorMessage="RegularExpressionValidator" ValidationExpression="[^0]{0-10},[^1]{0-13}" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
This doesn't work , but I want something like this.

Your regex [^0]{0-10},[^1]{0-13} means:
[^0] - Match anything but 0 (even a comma)
{0-10}, - Match literal string {0-10}, (because of the hyphen inside {...})
[^1] - Match anything but 1
{0-13} - Match literal {0-13} string.
If you plan to use only server-side validation, you can use .NET regex to validate
if it starts with 0 the length will be 10 , else will be 13.
:
^(0)?(?(1)\d{9}|\d{13})$
See demo
In case you also plan to use it on the client side, you will need a JavaScript regex flavor where you cannot use conditionals:
^(?:0\d{9}|\d{13})$
See another demo

[^0]{0-10} means from 0 up to 10 characters not equal to 0. The ^ does not have the same meaning in the beginning of the regular expression and later.
A regular expression for a string of 10 characters starting with 0 is ^0.{9}$.
The other is ^[^0].{12}$.
So together: ^(0.{9}|[^0].{12})$

Related

Custom regex validation ASP.NET Webforms

I have a custom validator that is supposed to prompt the user to removed certain characters if found in the textbox. However, the validation is coming up even when there are no matching characters in the textbox.
I have tested the regex before implementing it in asp.net but it cannot pass the validation.
asp:RegularExpressionValidator ID="revHarmfulCharacters" runat="server"
ErrorMessage="Please remove these characters where present >, <, /*, *\, --, |, {}"
ControlToValidate="txt_comment" ValidationExpression="[/^{}|<>(--)(/*)(*\/)(>=)]"
Display="Dynamic">
</asp:RegularExpressionValidator>
The regular expression should be shown for this case
But should not be shown for this case
This is a test of the regex that I did
Regular expression validation are written to match valid input. Since your regex pattern was not matched entire input string in both cases, so the error message is showed up in both cases.
You could try this pattern: ^[^/{}|<>(--)(/*)(*\/)(>=)^]+$
Edit the regex and test it.
Regex is ValidationExpression="[\^{}|<>(--)(*)(*\/)(>=)]" Try this
I try with this <bibin#212.com>. It is matched.
I try with this bibin#212.com. It is not matched.

Combine 2 or more regex in 1

I've 2 regular expression:
string regex1 = "(?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$)"; this will check for 'admin' substring in the given string and case is insensitive.
string regex2 = "^[^<>?]{5,100}$"; this will check for special char(^<>?) and length between 5 to 100 only.
I want a regular expression where both the regex can be validated at once with the use of only single regex.
Ex-
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtBox1" ErrorMessage="Validation Failed!"
ValidationExpression="(?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$)">
</asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="txtBox2" ErrorMessage="Length Validation Failed!"
ValidationExpression="^[^<>?]{5,100}$">
</asp:RegularExpressionValidator>
Q. Can we have a single "RegularExpressionValidator" that serves both the above functionality?
The (?i)(^(?!^.*?admin)(?!^.*?admin[admin\d]).*$) regex is too redundant, it is equal to (?i)^(?!^.*?admin).*$. It basically matches any string that contains no admin substring.
The ^[^<>?]{5,100}$ regex disallows <, > and ? in the string and sets string length limit.
Combining the two is done by replacing the .* in the first pattern with the consuming part of the second regex ([^<>?]{5,100}):
(?i)^(?!^.*?admin)[^<>?]{5,100}$
Details
(?i) - case insensitive mode on
^ - start of string
(?!^.*?admin) - no admin substring allowed anywhere after 0 or more chars other than line break chars, as few as possible
[^<>?]{5,100} - five to a hundred chars other than <, > and ?
$ - end of string.

Make input field validate a number range and a single character using regular expressions

This is a commonly asked question. But I am not getting the answer that I need. I had a look at MVC Validation make RegularExpression numeric only on string field (among many) for reference but I am not quite there yet.
I have an html5 input field bound to an MVC property. I want to make use of the [RegularExpression()] attribute but I am not getting the output that I need. I need my input to only take the following:
A single number between 0 and 7, or the % character
I tried the following:
[RegularExpression("^[0-7][%]")].
Where am I going wrong?
If you want to use range from 0 to 7. (Only single digit at a time) then use [0-7] and for % use [%]. To concatenate both the requirements(For or condition) use | in between two square brackets.
Your regular expression would be look like this
[RegularExpression("^[0-7]|[%]")]
^ at the starting of regular expressions, suggests that your string should starts with the number.
$ mentioned by #Stephen in comments states that your % must occurs at the end of string or expression.
Now its up to you, if you want string should be in D% format, where D is for digit and %, then use ^ in the beginning and $ at the end of regex. e.g. 5%
if this is not the case, then you can remove ^ and $ from the regex e.g %

ASP.Net: RegularExpressionValidator ValidationExpression to prevent `;` and `–` at a time

I want to make an ASP.Net RegularExpressionValidator ValidationExpression that will prevent to give input ; and –-.
I can do it for – and it is ValidationExpression="[^-]*".
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="**"
ValidationExpression="[^-]*"></asp:RegularExpressionValidator>
Above expression can prevent single – character. I will give permission for single – character. I will block for double – characters (--).
I need to prevent ; and – at a time.
Can anyone help me?
The Regex needs to get smarter. You want to block multiple hyphens, the m-dash, and semicolons.
So, try #"^(?=[^–;]*)((?!--).)*$"
Breaking it down:
The ^ matches the start of a line, and helps ensure that the validator is used to match the entire string.
The expression in the first set of parentheses will match any set of characters that do not include the m-dash and semicolon. You may want to substitute the hex value for the m-dash using the \x2014 escape sequence. It is defined as non-consuming with the ?=, meaning the Regex engine must match this pattern but will not advance its index when it finds the match, so the same set of characters will be tested for the next pattern as well.
The expression in the second set of parentheses is an inverse look-ahead; it will match any set of characters not containing two (or more) adjacent hyphens. This may be a bit slow, however; this regex basically forces the Regex engine to consider each character one at a time, looking ahead from that point to ensure the next character won't make match the inverse pattern.
The trailing $ marks the end of a line; together with the ^ it ensures that you are looking at everything in a single string (or line in multiline data) when determining a match.
Plug this into a Regex tester like Derek Slater's and play around with it to make sure it will stop all the scenarios you want.

Regular expressions for numbers

How can I build a regular expression that checks that it must be a number between 0 - 999999999.
Opted for the range validator.
The simplest thing would be [0-9]{1,9} That will accept all integers between 0 - 999999999.
If you want decimals or scientific notation, that can be done too though it's trickier and you might also want to look into some other tool to validate the number value.
Assuming you would want to match numbers that don't start with zero:
Then [1-9]\d{0,8} should do it.
If you mean control the number which range, can use rangevalidator
MaximumValue Specifies the maximum value of the input control
MinimumValue Specifies the minimum value of the input control
<asp:RangeValidator
ControlToValidate="tbox1"
MinimumValue="1"
MaximumValue="999999999"
Type="Number"
EnableClientScript="false"
runat="server" />
You could do this:
\d{1,9}
\d - digit
{1,9} - 1 to 9 repetitions
If you are trying to validate this inside a <asp:RegularExpressionValidator> then you need to do the same as has been hinted at so far in this thread but you also need a ^ and $ on each end so that it doesnt allow other characters to be added in around it.
^\d{1,9}$
Try this:
[0-9]*
It works for me.

Categories

Resources