Two validators for the same control not working - asp.net

I'm trying to use two validators for the same control as shown in below code
<asp:TextBox runat="server" ID="tbEmail" placeholder="Enter email" class="form-control" required=""></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"></asp:RequiredFieldValidator>
But the problem is the second validator works but the first validator doesnot. I tried flipping them but same thing happens.
What I have to do to make both of them work at the same time ?
Can you help me in this?

Add an ErrorMessage attribute to the regex validator like so:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbEmail" ValidationGroup="DRF" Display="Dynamic"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Please enter a valid email address"></asp:RegularExpressionValidator>

Related

Asp .net Validation controls

I have one text box and these asp validation controls on it.
<asp:TextBox ID="txtMinLot" runat="server" CssClass="form-control" MaxLength="10" CausesValidation="false"> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtMinLot" ErrorMessage="Please enter min lot for Bidding" Font-Names="Arial" Font-Size="11px" Display="Dynamic" SetFocusOnError="True" ValidationGroup="check" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator13" runat="server" ControlToValidate="txtMinLot" Display="Dynamic" ErrorMessage="Not a Valid number." Font-Size="11px" ForeColor="Red" SetFocusOnError="True" ValidationExpression="[0-9]*$" ValidationGroup="check" />
<asp:RangeValidator ID="rvLot" ControlToValidate="txtMinLot" runat="server" CssClass="result" Display="Dynamic" SetFocusOnError="True" ErrorMessage="Min Quantity should be less than Total Quantity and more than 0" Font-Size="11px" ForeColor="Red" MinimumValue="1" MaximumValue="100"></asp:RangeValidator>
But as i type something in character both range validator as well as regular expression validator message is showing in the page. But I want to show only one message at a time
In this case when you want the value in txtMinLot to be an integer number between 1 to 100, you don't need a regularexpression validator at all. you simply need to use a requiredfieldvalidater and rangevalidator; just like you did but with one change:
<asp:RangeValidator Type="Integer" ID
Type="Integer" will make sure user doesn't enter real numbers with decimal points and will only enter integers
Use regular expression "[1-9][0-9]?|100" and remove the range validator.
Another option is just to use a style (CSS) to hide the additional error messages. The exact rule will depend on what version of the validators you are using, so I'm not even going to guess, but if you post the HTML that is generated, it should be easy.

ASP.Net Prevent compare validator and regular expression validator from firing at the same time

I've got two text boxes like this:
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." />
It's two text boxes that have regular expression validators that constrain them to only allow positive real numbers. These work fine.
I also want the input in the second text box to be smaller than the first one. For that I have a compare validator.
When the user has correct numbers the compare validator works fine.
It's when they enter in anything that fails the second regular expression validator, that the compare validator also fires at the same time.
It doesn't matter what's in the first text box, valid input, wrong input, or even nothing. Both of the second validators fail.
Even though the validator is supposed to be comparing doubles.
Is there an easy fix for this?
I realize that this behavior is okay, because a validator is invalid when it should be, but the user would be seeing the incorrect error messages.
I've already done a solution involving custom validators and Javascript, and if it comes down to it then I'll have to do that again. But if that's the case then there's not much point to using a compare validator, since it'll never work with a regular expression validator.
Assign different validation groups to regular expression validator and compare validator and use ASP.NET inbuilt javascript function Page_ClientValidate() to check validation one by one.
<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2"/>
<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." ValidationGroup="Group3" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
In Javascript
<script type="text/javascript">
function Validate() {
var isValid = false;
isValid = Page_ClientValidate('Group1');
if (isValid) {
isValid = Page_ClientValidate('Group2');
}
if (isValid) {
isValid = Page_ClientValidate('Group3');
}
return isValid;
}
</script>
I think you should use Custom Validator for second textbox to confirm that Comparison logic occurs if regular expression validation passes. You could do it the following way-
<asp:Textbox id="textBox2" runat="server" text=""></asp:Textbox>
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "textBox2"
ErrorMessage = "Your Error Message"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
<script type="text/javascript">
function validateLength(oSrc, args){
// your validation logic here
}
</script>

RadMaskedTextBox ssn format

I have a RadMaskedTextBox for SSN while entering value in it, the first two characters are clearing up itself and getting RegularExpressionValidator message. Can some body help me in this.This problem exits in IE browsers only.
<span id="SSN" runat="server">*</span>Social Security Number</label>
<asp:RegularExpressionValidator ID="TaxId_RegEx"
runat="server" ControlToValidate="TaxId"
Display="None" ValidationExpression="^\d{3}\-\d{2}\-\d{4}$" ErrorMessage="Please enter a valid 9 digit SSN." Enabled="true"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="TaxId_ReqField"
runat="server" ControlToValidate="TaxId"
Display="None" ErrorMessage="Please enter a value for this required field."></asp:RequiredFieldValidator>
<radI:RadMaskedTextBox CssClass="box_SSN" SelectionOnFocus="SelectAll" ID="TaxId"
Width="85px" Mask="###-##-####" runat="server">
</radI:RadMaskedTextBox>
Try:
^\d{3}-\d{2}-\d{4}$
Instead of:
^\d{3}\-\d{2}\-\d{4}
This is working perfectly for me in IE:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1" Text="*">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ValidationExpression="\d{3}-\d{2}-\d{4}"
ControlToValidate="TextBox1"
ErrorMessage="Input valid SSN!">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Submit"/>
Not sure why you have
Display="None"

Required and RegularExpression Validators taking up space

I'm having issues with asp.net's field validators taking up space on my page. I've searched around, and documentation says to use Display="Dynamic" to keep the validators from taking up space. When I use this however, the error messages are always displayed.
What am I doing wrong?
I just want the error messages to display when the user either clicks the "Save" button, or loses focus on a textbox. And I don't want the validators to take up space.
<p>Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGgroup="Email"
ErrorMessage="Please enter an email"
ControlToValidate="TextBoxEmail" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
ValidationGroup="Email"
ControlToValidate="TextBoxEmail"
ErrorMessage="Please enter valid email"
runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" />
<p>Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:LinkButton ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" />
Only thing I notice in your code is you have a typo in ValidationGgroup="Email". It should be ValidationGroup="Email"
Other than that, Display="Dynamic" works for me.
After clicking, submit button -
<p>
Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Email"
ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email"
ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" />
<p>
Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TextBoxEmail"
ControlToValidate="TextBoxEmail2" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Must match." ValidationGroup="Email"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="Email"
ErrorMessage="Please enter an confirm email" ControlToValidate="TextBoxEmail2" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" ValidationGroup="Email" runat="server" Text="Save"
OnClick="linkbuttonSave_Click" />
You can set the property Display to "Dynamic" or "none", the second one will cause the Error message to be displayed only in the validation summary.
I got it to work great by deleting the "ValidationGroup=.." . Havent looked what up what this does but it works great without it and not sure why it would be needed (given the validator controls have the control id of the email textbox)

RegularExpressionValidator shows an exception that following field cannot be empty

I have a textbox txtemailid which has two validator RequiredFieldValidator and RegularExpressionValidator as:
<asp:TextBox ID="txtEMailId" CssClass="Text1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFVtxtEMailId"
runat="server"
ErrorMessage="*"
Display="Dynamic"
ForeColor="Red"
ControlToValidate="txtEMailId"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="REVtxtEMailId"
runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"
ErrorMessage="Invalid Email Format"
ForeColor="Red"></asp:RegularExpressionValidator>
it shows following error
The ControlToValidate property of 'REVtxtEMailId' cannot be blank.
You not have specified which control should be validated .Try this
<asp:RegularExpressionValidator ID="REVtxtEMailId"
runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display="Dynamic"
ErrorMessage="Invalid Email Format"
ForeColor="Red" ControlToValidate="txtEMailId"></asp:RegularExpressionValidator>
You have not provided ControlTo Validate Property in Regular Expression Validator.Add this property in REVtxtEMailId

Resources