I'm embarrassed to ask but I've been at this for hours trying multiple solutions with no luck.
I'm trying to validate the file name used by my asp.net FileUploadControl.
All I want to do is test if the file starts with "RFTrack".
<asp:FileUpload id="FileUploadControl" MaxLength="75" runat="server" CssClass="input" />
<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
ErrorMessage="Must start with 'RFTrack'" ValidationExpression ="^rftrack"
ControlToValidate="FileUploadControl" >
</asp:RegularExpressionValidator>
Using this validation, I can not get any test files starting with "RFTrack" to pass the validation. I have even tried using a file just named "rftrack" to no avail.
I used multiple online Regex testers that have confirmed that my regex is correct. I have also tried things like
^(rftrack|RFTRACK)
With no luck. I'm sure I'm just missing something obvious.
Related
I have 2 ASP.NET regular expression validators:
^\s*[-+.'\w]+#\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$ check whether the email address is valid
#(adres.pl|vp.pl) check if value contains any of these strings
The problem now is:
The strings in bullet 2 above should NOT be allowed, so o email address "john#adres.pl" should not be allowed.
However, the validators are only positive, meaning that they check if they DO contain the structure.
So when someone currently enters "test#test.com", I get "invalid value"
My code below:
<asp:TextBox ID="tbEmail" runat="server" />
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="emailinvalid" ValidationExpression="^\s*[-+.'\w]+#\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="rev1" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(adres.pl|vp.pl)" ID="rev2" runat="server"/>
I was initially thinking of combining expressions 1 and 2, but then I wouldn't know which one fails, and don't know which error to display.
What I would expect is:
"35435gd" -> emailinvalid
"john#adres.pl" -> not allowed
"john#gmail.com" -> all validators are ok
Or perhaps validator rev2 should only execute if rev1 does not throw an error, so "stacking" for a lack of a better term, which a: seems cumbersome and b: I wouldn't know how to do it.
How can I solve for this?
UPDATE 1
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="invalid" ValidationExpression="^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="RegularExpressionValidator5" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(?!(?:adres|vp)\.pl\b)" ID="RegularExpressionValidator4" runat="server"/>
"gddg" results in "not allowed", whereas I'd expect "invalid".
When I switch the order of these validators, both errors "invalid" and "not allowed" show.
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="invalid" ValidationExpression="^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$" ID="RegularExpressionValidator5" runat="server"/>
<asp:RegularExpressionValidator ControlToValidate="tbEmail" ErrorMessage="not allowed" ValidationExpression="#(?!(?:adres|vp)\.pl\b)" ID="RegularExpressionValidator4" runat="server"/>
You can use a single pattern and you might write your existing pattern as:
^\s*[-+.'\w]+#(?!(?:adres|vp)\.pl\b)\w+(?:[-.]\w+)*\.(?!co\s*$)\w{2,}\s*$
The part excluding either adres.pl or vp.pl can be written as #(?!(?:adres|vp)\.pl\b) excluding those matches directly after the #.
You could also use #(?!(?:adres|vp)\.pl\s*$) if that is the last part of the email address just like you currently do for this part \.(?!co\s*$)
See a regex demo.
Note that your pattern would allow leading and trailing whitespace chars for the e-mail address.
I have a text box where a user enters their email address. I need to prevent people using certain email addresses, as this is for corporate (B2B) use.
Can anyone help me with the RegEx which would return false if email addresses contain #gmail or #yahoo?
So far I have this (thanks to #Sabuj) #(yahoo|gmail)\. but when placed into a RegularExpressionValidator it doesn't work:
<asp:RegularExpressionValidator ValidationExpression='#(yahoo|gmail)\.' runat="server" ControlToValidate="txt_email" />
Having read MSDN for more info, I've also tried this but it still returns true regardless of the content entered:
<asp:RegularExpressionValidator ValidationExpression='^(#(yahoo|gmail)\.)$' runat="server" ControlToValidate="txt_email" />
Since e-mail addresses have a complex syntax (more complex than most people realise, for instance, they can contain comments [RFC 822 ยง 3.4.3]), I'd suggest not using regex at all for this. Instead, use a "proper" e-mail parser, then ask the parser for the domain part of the address.
Use this:
<asp:RegularExpressionValidator ValidationExpression=".*#(?!(yahoo|gmail)).*" ControlToValidate="txt_email" runat="server" ErrorMessage="Yahoo and Gmail disallowed"></asp:RegularExpressionValidator>
The validation expression property should be set to match the entire string.
But my regex .*#(?!(yahoo|gmail)).* matches the whole email. So it works :)
You don't need ^ or $ since the string is gonna be a single line.
Also don't forget to add type="email" to your txt_email. It will automatically take care of whether it is a valid email or not.
If the error msg appears, then it isn't valid, but if it doesn't appear, then it is absolutely valid.
I've come up with ^.*#(?!(yahoo|gmail)).*$
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="^.*#(?!(yahoo|gmail)).*$" runat="server" ControlToValidate="txt_email" Text="No free email accounts allowed" />
This will allow any text to pass the validator that doesn't contain #yahoo or #gmail.
Don't forget to check Page.IsValid in your code behind, and to include an <asp:ValidationSummary runat="server" /> in your .aspx.
You can use this regex to check whether the mentioned emails are containing or not:
#(gmail|yahoo|mailinator|guerrillamail|dispostable)\.
Another "I am pretty sure I know the answer, but I'll ask anyway just in case" question.
(The answer I think being "no, there isn't")
Is there any built-in way to chain validators in ASP.NET (.Net 4.0) without the need to write <asp:CustomValidator> code? So that one validator will only be checked if another validator has passed it's validation?
For instance, say you have two <asp:DropDownList> controls, both with the exact same options, including a "blank" entry at the start (because you don't want anything pre-selected, that is a decision the user must consciously make)...
<asp:DropDownList runat="server" id="dd1">
<asp:ListItem Value="-1" Text=""/>
<asp:ListItem Value="1">One</ListItem>
<asp:ListItem Value="2">Two</ListItem>
...
</asp:DropDownList>
<asp:DropDownList runat="server" id="dd2">
... with identical list of items as dd1
I would put two <asp:CompareValidator> controls, one associated with each of the above <asp:DropDownList> controls
<asp:CompareValidator runat="server" id="val1" ControlToValidate="dd1"
ValueToCompare="-1" Operation="Not Equal" ErrorMessage="Select DD1" />
<asp:CompareValidator runat="server" id="val2" ControlToValidate="dd2"
ValueToCompare="-1" Operation="Not Equal" ErrorMessage="Select DD2" />
These work fine, and shows the appropriate error if the original "blank" entry is the selected item.
However, I need the two items to be different, so I add another <asp:CompareValidator>...
<asp:CompareValidator runat="server" id="val3" ControlToValidate="dd1"
ControlToCompare="dd2" Operation="Not Equal" ErrorMessage="DD2 must be different to DD1" />
The result of this is that if both DD1 and DD2 have the "blank" as their selected items, all three validators will fire on a form submit. This is incorrect, as val3 should only be validated if val2 has successfully validated.
The obvious solution is to use a <asp:CustomValidator> instead, and handing the situation myself... but I thought I'd ask if there was a built-in way of making val3 only validate if val2 was valid.
Given a Textbox, that should contain a numeric value:
<asp:TextBox ID="txtHoldsAnInt" runat="server" />
In our codebase, generally a RegularExpressionValidator is used for all validation (except required fields) such as the following:
<asp:RegularExpressionValidator ErrorMessage="..." ControlToValidate="txtHoldsAnInt"
Text="*" runat="server" ValidationExpression="^[0-9]{1,8}$" />
Alternately, a RangeValidator could be used to get the same result:
<asp:RangeValidator ErrorMessage="..." ControlToValidate="txtHoldsAnInt"
MinimumValue="0" MaximumValue="99999999" Type="Integer" runat="server" />
Does the RegularExpressionValidator have an advantage over other validators, even when another validator would work? Are there any advantages to always using a RegularExpressionValidator?
If you are familiar with regular expressions, then I would recommend using the <asp:RegularExpressionValidator. You are correct that there is no noticeable difference when they are equal to each other, but the RegularExpressionValidator allows for easy editing later on when specifications change. For example with a RegularExpressionValidator it would be easy to allow for a percent sign at the end or any other change that might be requested.
In terms of functionality/end result, there is no difference between using the two. I would suggest using the range validator other validators if the situation permits and it satisfy your needs. It is much easier for someone to understand <asp:RangeValidator than to understand ^[0-9]{1,8}$. While the RegularExpressionValidator will give you a much higher degree of control, if that control is not needed, then go for the simple solution and ease your maintenance efforts.
In asp.net, I need to add a validator to a textbox that forces the input to be numbers.
Is this built in?
I have already added a required field validator to the textbox.
You could use a Regex Validator to ensure the text is numeric
I think the regex would be
[0-9]*
e.g.
<asp:TextBox ID="tbxNumbers" runat="server" />
<asp:RegularExpressionValidator ID="revNumericValidator" runat="server"
ValidationExpression="^[0-9]*$" ControlToValidate="tbxNumbers" ErrorMessage="Must be Numeric" />
EDIT:
As the other two posters also pointed out you can also use \d to represent a Numeric Character
<asp:RegularExpressionValidator runat="server"
ControlToValidate="numbersOnlyTextBox"
ErrorMessage="Enter only numeric characters."
ValidationExpression="^\\d+$" />
Use a range validator.
<asp:TextBox ID="MyTextBox" MaxLength="4" Width="75"
Text="0" runat="server"></asp:TextBox>
<asp:RangeValidator ID="MyRangeValidator" Display="Static" Type="Integer"
MaximumValue="9999" MinimumValue="0" EnableClientScript="true"
ControlToValidate="MyTextBox" runat="server" SetFocusOnError="true"
ErrorMessage="Ooops"></asp:RangeValidator>
This permits you to use numbers with decimal places (by using Type="Double" or "Currency"), or other kinds of numbers that Windows recognizes.
Check MSDN for more info on the Range Validator Control.
I think there needs to be more clarification of the requirements here. What kind of numbers are we talking about? Positive integers? Any integer? A number with a decimal place? What about commas in the number (1,000)?
I recommend a RegularExpressionValidator to do your work, but these questions make a difference when it comes to which RegEx you use.
In order to provide a better user experience, another thing to add is an AjaxToolkit FilteredTextBox extender, with a FilterType of either "Custom, Numbers" or just "Numbers". The first choice is for when you want to be able to specify decimal points and negative numbers. In that case you must also specify the ValidChars attribute with something like "-.". This will stop a user from entering characters that are not going to make up a valid number such as -123.45 . Note that it does not stop the user from entering the '-' & '.' in incorrect places e.g. "2-..-3" can still be entered. You will need the validators mentioned in other answers to catch these cases.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Custom, Numbers"
ValidChars="-." />
Or
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server"
TargetControlID="numbersOnlyTextBox"
FilterType="Numbers" />