Regex Negative Lookahead - asp.net

I have a situation wherein I need to search through a VS project for any control that does not have a MaxLength property defined.
For example:
<asp:TextBox ID="txtName" runat="server" MaxLength="50" Text="Enter Name" />
<asp:TextBox ID="txtOther" MaxLength="25" runat="server" />
<asp:TextBox ID="MaxLength" runat="server" />
<asp:TextBox ID="txtMisc" runat="server" Width="100" />
Does anyone have a suggestion for a regular expression pattern that will find the controls that do not have a MaxLength defined?
My first attempt at this, which seems to work, seems imperfect at best...
<asp:TextBox.*?M(?!axLength=).*?/>
I would love to see a better solution.
Note: the Visual Studio search chokes on my pattern above. I was forced to use a different application to actually search using this pattern

I think this is what you were trying for:
<asp:TextBox(?:(?!MaxLength=|>).)*/>
The . consumes one character at a time, but only after the lookahead has determined that it's not > or the beginning of MaxLength=. Note that you must exclude > in the lookahead, or it will keep looking for MaxLength= beyond the end of the current element. For example, when applied to
<asp:TextBox ID="txtMisc" /><asp:TextBox MaxLength="50" />
...you want it to match the first tag, but it doesn't because the lookahead sees MaxLength= in the second element. A non-greedy quantifier like .*? will not prevent that from happening. It might seem like it's working correctly, but that's only because the tags usually appear on separate lines, and the . doesn't match newlines.
The Visual Studio version would be:
\<asp\:TextBox(~(MaxLength=|\>).)*/\>
<, > and : all have special meanings in VS regexes and have to be escaped, and ~(...) is the VS syntax for a negative lookahead.

Try this... negative lookahead on "MaxLength" within the element
\<(?!.*MaxLength[^/>]*)[^/>]*/\>

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>

Limit input of regular expression validator

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}$"

regular expression - word surrounded by no matter

I'm looking for a regular expression that which will match a string ##test## in a text. The string can be surround by any word or nonword character, or white-space, or newline - so everything can be one the left and on the right of the string, no matter.
I need to use it in a asp.net RegularExpressionValidator control for ValidationExpression.
Thanks in advance for help !
Regards.
<asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server" ControlToValidate="TextBox1"
Display="Dynamic" ValidationGroup="Mail" ErrorMessage="Insert !"
ValidationExpression="(\w|\s|\n)##test##(\w|\s|\n)" >*</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button"
ValidationGroup="Mail" Onclick="Button1_Click" />
This should do it:
ValidationExpression="[\s\S]*##test##[\s\S]*"
I can see three problem with your regex, all having to do the (\w|\s|\n) portion:
It will match exactly one character. You need it to match zero or more characters; adding the * quantifier does that.
It's needlessly redundant and gratuitously inefficient. [\w\s] matches the same things: a word character (\w) or a whitespace character (\s, which already includes \n). And whenever you have a choice between using and alternation ((a|b|c)) or a character class ([abc]), the character class should always be the first tool you reach for. It may look like a trivial choice, but it can have a huge impact on performance.
It leaves out a lot of characters, most notably punctuation characters like !, +, ., etc..
If you're wondering why I used [\s\S]* and not .*, it's because the validation can be performed either in the browser or the server. In JavaScript regexes, as in most other flavors, the . metacharacter doesn't match newlines. Most of the others also support a "single-line" or "dot-matches-all" mode, but not JavaScript.
Of course, you could just force it to run on the server only, but you might as well get into habit of dumbing down your regexes to JavaScript's level when you can. :-/
Something like this?
.*(##test##).*

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])$

Regular Expression fails in IE, but works in Chrome and Firefox?

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]).

Resources