currently I have a strange problem with RangeValidator.
I have a TextBox declared like this :
<asp:TextBox ID="textBox1" runat="server" type="number"></asp:TextBox>
and a RangeValidator declared like this :
<asp:RangeValidator runat="server"
ID="rangeValidator1"
ErrorMessage="Insert between 1-10000"
ControlToValidate="textBox1"
MaximumValue="10000"
MinimumValue="1">
for some reason it only valid when I entered these numbers :
1
10
100
1000
10000
the rest numbers between 1-10000 (ex.: 2-9, 11-99, etc.) simply cause the validator to fail.
why is this happen?
in order to validator to work with a range of numbers, the Type should be declared as Integer.
this is something I miss.
<asp:RangeValidator runat="server"
ID="rangeValidator1"
ErrorMessage="Insert between 1-10000"
ControlToValidate="textBox1"
MaximumValue="10000"
MinimumValue="1"
Type="Integer">
Related
I have two DropDownLists populated with Year-Dates, and I want to show an error message in the case where the second ddls value is less than the first ddls value.
This is the code I have used so far, and it doesn't work:
<asp:CompareValidator
ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger"
ValidationGroup="Save" ControlToValidate="ddlEndYear" Display="Dynamic"
ValueToCompare="ddlStartYear" ErrorMessage="Greater Than" SetFocusOnError="true">
</asp:CompareValidator>
You have to specify the ControlToCompare and the Operator:
<asp:CompareValidator
ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger"
ValidationGroup="Save"
ControlToValidate="ddlEndYear" Display="Dynamic"
ControlToCompare="ddlStartYear"
Operator="GreaterThanEqual"
Type="Integer"
ErrorMessage="The end year must be greater/equal the start year" SetFocusOnError="true">
</asp:CompareValidator>
You haven't specified the type, specify the type of data which would be in the textboxes :
<asp:CompareValidator
..........
..........
ControlToValidate="ddlStartYear"
ControlToCompare="ddlEndYear"
Operator="GreaterThanEqual"
Type="Integer">
</asp:CompareValidator>
You might want to have a look at this tutorial
I have a Textbox on my webform, for instance 2012 entered in single text box the following text box must enter 2014 or else should give error.Iam trying to validate it by using compare validation but am unable to meet the exact condition what i want.Can i know how can it be done?thnks in advance
I cant fine wha'ts the hard part here...
int number=Convert.ToInt32( textBox1.Text);
if(number==2014)
Response.Write("good");
else
Response.Write("Bad number");
More easy then that?
If your validator should ensure that the text in two TextBoxes is equal use a CompareValidator with appropriate ControlToValidate and ControlToCompare:
<asp:TextBox id="Txt1" runat="server">
</asp:TextBox>
<asp:TextBox id="Txt2" runat="server">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="Txt2"
ControlToCompare="Txt1"
ErrorMessage="Text in second textbox must be equal to text in first textbox!">
</asp:CompareValidator>
If you also want to ensure that only integers can be inserted, use DataTypeCheck and Integer:
<asp:CompareValidator ID="CompareValidator2" runat="server"
ControlToValidate="Txt2"
Type="Integer" Operator="DataTypeCheck"
ErrorMessage="Text in second textbox must be an integer!">
</asp:CompareValidator>
I have a form of three text boxes having all three inputs only be numbers:
<asp:TextBox ID="TextBox1" runat="server" TextMode="Phone" />
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" />
<asp:TextBox ID="TextBox3" runat="server" TextMode="Password" />
I was wondering if there is a possibility of having two (or more) TextMode for an <asp:TextBox />.
For example:
<asp:TextBox ID="TextBox2" runat="server" TextMode="Phone Password" />
This isn't possible.
ASP.NET TextBox's render as HTML input elements. The TextMode property is used to determine the type attribute.
The input's type attribute cannot contain a mixture of values, and therefore neither can the ASP.NET TextBox.
TextMode attribute accepted only three name
SingleLine - one line textbox
Multiline - Textbox with multiple lines
Password - One line textbox that masks the input
you can use .net Validation Control or jquery validation :)
I am sure that this will be my fault, as I can't believe that the RangeControlValidator is incorrect, but I can't see why this is not working.
<ItemTemplate>
<div class="reportItem">
<div class="l1" >New</div>
<div class="l2"><asp:Textbox runat="server" name="numberOnly" ID="tbNew" CssClass="compliance" onblur="calculateCompliance(this);"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required Value" ControlToValidate="tbNew" Display="Dynamic" Text="*" />
<asp:RangeValidator ID="RangeValidator3" runat="server" ErrorMessage="Must be a Positive Number" ControlToValidate="tbNew" Display="Dynamic" Text="*" MinimumValue="0" MaximumValue="1000" Type="Integer" />
</div>
The Range validator is in a repeater. I am trying to limit the value in the text box to between 1 and 1000.
If I allow IE autocomplete to fill the box it works fine, but if I manually enter the value, then I get the red star and an Invalid result.
Initially I thought that this was because it was in a repeater, and it was getting confused over names, but the Required Field Validator works, so I no longer believe that this is the case.
So, can anyone see anything wrong with the code sample?, is somethign very odd going on?
(the onblur does nt affect the value in that cell, but it does read it to do a sum elsewhere)
I have an asp:textbox with both required and range validators attached to it, where the code looks like this:
ASP:
<asp:TextBox ID="textBox1" runat="server" CausesValidation="true"></asp:TextBox>
<asp:RangeValidator ID="rangeValidator1" runat="server" ControlToValidate="textBox1" MaximumValue="1" MinimumValue="0"
ValidationGroup="valid" ForeColor="Red" ErrorMessage="Out of Range" />
<asp:RequiredFieldValidator ID="requiredValidator1" runat="server" ControlToValidate="textBox1"
ValidationGroup="valid" ForeColor="Red" ErrorMessage="Cannot be blank" />
And when the page is dynamically loaded (after a quick callback), I have code that is supposed to change the MaximumValue of the RangeValidator to more specific value. Here is the code for that:
rangeValidator1.MaximumValue = GetMaxValue(params).ToString();
Now, I have set a breakpoint, and rangeValidator1.MaximumValue is being set correctly, however, when the page loads, and I look at the compiled client side javascript, it appears that the maximum value is still only 1.
What confuses me more is that any integer typed in will pass, as long as the first digit is a '1'. So if the maxValue is supposed to be something like "1234567", "1" will match, as will "12345678910". But "2" will not. Nor will "3000" or "46000".
Has anyone else had a similar issue with RangeValidators on Textboxes?
The RangeValidator handles validation for multiple types. You should make sure to set the Type to Integer. or what ever is appropriate.