Using compare validator - asp.net

Is there a way in using compare validator that when you use lessthanequal or greaterthanequal operator it automatically treat the type as int? Let's say adding additional tag in web config or any setup? thanks

CompareValidator has a Type property that you could use.
<asp:CompareValidator ID="CV_GreaterTen"
Type="Integer"
Operator="GreaterThanEqual"
ValueToCompare="10" runat="server" ErrorMessage="Must be at least 10" ControlToValidate="Textbox1">*
</asp:CompareValidator>
Apart from that, there is no automatism.
By the way, such magical(mostly hidden) settings would be a good source for future problems.

Related

Simple VB.NET RegularExpressionValidator not working

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.

RegEx stops working when placed into ValidationExpression

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

Should Regular Expressions be used over other validation methods?

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.

Regular expression for e-mail validation not working

I've set up a regular expression validator to validate all e-mail addresses that are entered into a textbox on one of my forms the only problem is it's not working in a weird case.
Here's my code
<asp:TextBox ID="tbEmail1" runat="server" />
<asp:RegularExpressionValidator Display="Dynamic" ID="rgv1" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail1" ErrorMessage="Invalid email address" />
The case that this is not working is when an e-mail address is entered like this
jamie-taylor-#hotmail.co.uk
Now I wasn't aware that e-mail addresses could be set up in this way but i've just set one up and it seems to be fine except the fact that I cannot input this into my form without it telling me it's invalid
Any ideas?
Thanks in advance
Comparing E-mail Address Validating Regular Expressions. This page has both regexes and e-mail addresses to test with.
It is generally better to make your regex not very strict. Better to have one or two e-mails bounce back than that your customers can not create an account.
let's enhance the regex :
\w+([-+.']\w+[-]*)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
edit
for #Sjoerd :
\w+([-+.']\w+[-]*)*#\w+([-.]*\w+)*\.\w+([-.]\w+)*
use the below expression may help you
\w+([-+.']\w+[-])*#\w+([-.]\w+)*\.\w+([.]\w+)*

In asp.net, I need to add validator to textbox that forces the input to be numbers

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" />

Resources