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.
Related
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>
Basically, I've got code that looks like this:
<div>
<asp:TextBox ID="txtBox1" class="inline-block" runat="server" />
<asp:RegularExpressionValidator ID="regExTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />
<asp:CompareValidator id="cmprTxtBox1" runat="server" ControlToValidate="txtBox1" ControlToCompare="txtBox2" Type="Double" Display="Dynamic" Operator="LessThanEqual" Text="Error: txtBox1 is greater than txtBox2." />
</div>
<br />
<div>
<asp:TextBox ID="txtBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />
</div>
I've got two text boxes that use regular expression validators in order to force the user to only enter integers or decimals. I also want the input in txtBox2 to be larger than txtBox1, so I use a compare validator on txtBox1 in order to check for when the number in txtBox1 is larger than txtBox2.
The issue that I'm running into is that my CompareValidator always triggers when the RegularExpressionValidator triggers. The CompareValidator is perfectly content when there are normal numbers in txtBox1 and nothing in txtBox2. But it fails whenever the RegularExpressionValidator fails, regardless of what's in txtBox2.
Of course, the system functions correctly, because either way the user won't be able to enter any values when the validators are returning invalid. But I don't want the user to see unnecessary error messages.
If there isn't any solution I think I could implement a custom validator.
Please Check By Using Validation Group Property.....
For and Validation Control and put Same Validation Group To Submit Button so it will Validate On Button Click.....
I hope It Will Helps You.....
I figured out a workaround.
For the text box with the compare validator I added an 'onchange' call to a javascript method. In that method, it checks whether or not the regular expression has failed. If it has, it sets the compare validator to true and then updates the display.
The implementation in javascript looks like this:
function txtBoxChanged() {
var txtBox1 = document.getElementById('<%=txtBox1.ClientID%>').value;
var rgx = new RegExp(/^0*[1-9]+\d*(\.\d+)?$|^0*\.0*[1-9]+\d*$/)
if (!(rgx.test(txtBox1))) {
document.getElementById('<%=cmprTxtBox1.ClientID%>').isvalid = true;
ValidatorUpdateDisplay(document.getElementById('<%=cmprTxtBox1.ClientID%>'));
}
}
And my HTML:
<div>
<asp:TextBox ID="txtBox1" class="inline-block" runat="server" onchange="txtBoxChanged()" />
<asp:RegularExpressionValidator ID="regExTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />
<asp:CompareValidator id="cmprTxtBox1" runat="server" ControlToValidate="txtBox1" ControlToCompare="txtBox2" Type="Double" Display="Dynamic" Operator="LessThanEqual" Text="Error: txtBox1 is greater than txtBox2." />
</div>
<div>
<asp:TextBox ID="txtBox2" runat="server" onchange="txtBoxChanged()" />
<asp:RegularExpressionValidator ID="regExTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />
</div>
I had to make it so both text boxes were calling this method. Otherwise even when the regular expression failed on txtBox2 the compare validator would show up. Which makes sense because it is looking at both text boxes.
Sorry Im a noob. is there a way to get a value of a td (which is outside of a templatefield) as a value of "ControlToCompare" of an asp:CompareValidator? i need to compare a date field to an existing given date. Pls help!
I know this is wrong. but heres my code
<asp:CompareValidator ID="CompareValidator1" ControlToCompare="<$("#spanDateofInjury span").text()" ControlToValidate="txtDateFrom"
Type="Date" Operator="LessThanEqual"
ErrorMessage="From Date should be less than or equal to To Date"
runat="server" Display="None" EnableClientScript="true" ValidationGroup="PaymentsDetailUpdate"></asp:CompareValidator>
As the span is read-only you can try this method. Place a hidden field inside the TemplateField and compare with it like this
<asp:HiddenField ID="hdnDateOfInjury" runat="server" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="hdnDateOfInjury"
ControlToValidate="txtDateFrom"
Display="None"
EnableClientScript="true"
ErrorMessage="From Date should be less than or equal to To Date"
Operator="LessThanEqual"
Type="Date"
ValidationGroup="PaymentsDetailUpdate">
</asp:CompareValidator>
I have search textbox,which has default value Enter Month to View Profit. When I click search button without entering any data, the default value of textbox is posted to server for search. I want that RegularExpressionValidator do not validate default value of textbox.
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!"
>
</asp:RegularExpressionValidator>
The default for all validators except the RequiredFieldValidator control if you post with empty field the validator will not trigger
you must use required field validator with other validators to prevent the postback to happen
from MSDN
Special-Case Validation Results for ASP.NET Server Controls
EDIT
Also if you add your controls as in your question it should work but if there is other controls like for example other button you should set the validationGroup Property to the group that you want to work together
ValidationGroup="vGrp"
and your code will be like this
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server" ValidationGroup="vGrp"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" ValidationGroup="vGrp" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
ValidationGroup="vGrp">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!" ValidationGroup="vGrp"></asp:RegularExpressionValidator>
from MSDN about validationGroup
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>