asp.net range validator on textbox - c#

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.

Related

C# ASP.NET Range Validator only accept several value

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

how to put validation for textbox with asp.net

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>

Multiple TextModes in ASP.NET

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 :)

comparevalidation for text box value and hidden field value

<asp:TextBox ID="txtAppSanctionLimit" runat="server" onblur="calcCustDebtEquity()"> </TextBox>
<asp:HiddenField ID="hfAppReqeustAmt" runat="server" Value="0" />
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Proposed Limit is never greater than Request Limit" ControlToCompare="txtAppSanctionLimit" ControlToValidate="txtRequestLimits" Operator="GreaterThan"
ValidationGroup="Report3" Display="none" ></asp:CompareValidator>
<asp:Button ID="btnLimtUpdate" runat="server" Text="Update" ValidationGroup="Report3"
onclick="btnLimtUpdate_Click"/>
<asp:ValidationSummary ID="ValidationSummary3" runat="server" ShowMessageBox="true" ShowSummary="false" ValidationGroup="Report3" />
Above all has been used in my defalt.aspx page under Visual Studio 2010.
I want to show Validation Message if txtAppSanctionLimit value is greater than hfAppReqeustAmt value. note that hfAppReqeustAmt value get from database with query.
how can I solve this problem.
The accepted answer is completely incorrect!
Simply put you can't use a CompareValidator with a HiddenField. You need to use a CustomValidator.
See: asp:RequiredFieldValidator does not validate hidden fields
The attribute for the target
ControlToCompare="txtAppSanctionLimit"
should probably be pointed at your hidden field.
ControlToCompare="hfAppReqeustAmt"
and the attribute for the ControlToValidate should be pointed to your user data entry field.
ControlToValidate="txtAppSanctionLimit"
as it is currently pointing to a control that is not shown in the example code.
EDIT: Per MSDN for the CompareValidator.ControlToCompare -
If the control to compare is hidden or is inside a container (such as
a Panel control) that is not visible, the validator performs
server-side validation only. The validator client script supports only
visible controls.

RangeControlValidator Misbehaving

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)

Categories

Resources