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.
Related
I'm trying to add validation to a form field so that if it contains a certain word, an error is displayed. At the moment I have this:
Validation.Add("field-1", Validator.Regex("^((?!TEST)(?!test).)*$", "Field may not contain 'test'"));
This works fine for "TEST" and "test", but won't prevent someone entering "tESt".
I've tried adding the case insensitive flag, but this made the regex stop working completely:
Validation.Add("field-1", Validator.Regex("/^((?!TEST)(?!test).)*$/i", "Field may not contain 'test'"));
I also read here that (?i) can be used to ignore case, but that didn't work either - maybe I'm putting it in the wrong place:
Validation.Add("field-1", Validator.Regex("^((?i)(?!TEST)(?!test).)*$", "Field may not contain 'test'"));
Is this doable without adding every possible variation of "test"?
Use an instance of the Regex class with a RegexOptions.IgnoreCase parameter.
Regex validator = new Regex(#"TEST", RegexOptions.IgnoreCase);
if(validator.IsMatch(formValue))
{
// Do something about this
}
It appears that that your regex is handled on the client side. JavaScript RegExp does not support (?i) inline modifier (it does not support any inline modifiers, even the XRegExp library), so the only way out is to spell out all the letter cases using character classes:
Validation.Add("field-1", Validator.Regex("^(?!.*[Tt][Ee][sS][tT]).*$", "Field may not contain 'test'"));
Note that your tempered greedy token is too resource consuming a pattern, it is easier to use a simple negative lookahead here. If you need to support multiline strings, replace the . with [\s\S] character class.
Details:
^ - start of string
(?!.*[Tt][Ee][sS][tT]) - there cannot be any Test or TesT, etc., after any 0+ chars
.* - any 0+ chars, as many as possible
$ - end of string.
This is very basic and not thorough validation I know but the textbox needs to accept for example name#email.com as well as 4556-222-44444.
I've tried
<asp:RegularExpressionValidator Display="Dynamic" runat="server" ControlToValidate="txtAddToBlacklist"
ErrorMessage="Invalid" ValidationExpression="^[\-\#]$"></asp:RegularExpressionValidator>
This attempt included the escaping backslashes to no avail.
My logic for using "[-#]" to check for either to be present came from this answer.
I know that the hyphen has to be at the beginning or the end but as it's only two characters, I don't think this is the issue.
The ^ and $ are included as that seems to be recommended practice to prevent malicious extras being appended.
Must be missing something though so any help is appreciated!
The ^ and $ are resp. the start and end of the string. This means that your regex just tests against the string "#" and "-".
If you use ^.+[-#].+$, you will test against strings containing the mentioned characters inside the string.
For multiline you might want to try something like this ^(.+[#-].+\n?)*$. (There's still some puzzling needed for this one. I added the ? after the newline because it should not be mandatory on the last line, but this way also "bla#bla.com#test.lan" will test positive.) Tweak it to fit your needs.
I'm not a regex-king, but use an online regex tool, such as http://www.regexr.com/, to build and test my expressions.
As a side-note: To gain regex skills you can try to solve these https://regexcrossword.com/
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})$
I am trying to scrape a page that has Hebrew text on it. It contains the following piece of HTML:
<div id="AgeRating">דירוג גיל: 12+</div>
I just want the 12+ part here (in fact: I only want the '12' part). I am currently doing to with this piece of regex for other languages:
new Regex(#"<div id=""AgeRating"">.*(\d{1,2})\+</div>", RegexOptions.Compiled);
But I just can't get this to match. I tried all the regex options like RightToLeft, CultureInvariant, SingleLine, MultiLine, etc. but nothing works. It does work fine with plenty other languages though.
Note: I'm aware of HtmlAgilityPack for proper parsing of HTML. This is question about why seemingly correct RegEx fails to match particular string (as this a sample I have currently).
This regular expression works for me:
<div id="AgeRating">.*?(\d{1,2})\+
This returns 12. I added a ? to .* to make the dot not greedy.
I think the thing that is throwing you off is that you have a hidden character (perhaps a Hebrew character?) after the plus sign. The following also works for your string (notice the dot after the plus sign, which accommodates your hidden character):
<div id="AgeRating">.*?(\d{1,2})\+.</div>
You also do need the ? after .* as I mentioned above in order to prevent the regular expression from returning 2 instead of 12.
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.