how to put validation for textbox with asp.net - c#

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>

Related

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

Restricting a text box to enter only numeric values and not alphabetical & alphanumeric values

I have 2 text boxes viz. txt_Long and txt_Lat. I want that only numeric values be able to be inserted into those text boxes and not numeric or alphanumeric values. If tried to insert any other values except numeric value, a validation violation message should be displayed in red color just under the respective text boxes.Can any one please help ?
Use compare validator to check the value entered is number or not .
<asp:TextBox ID="TextBox" runat="server"></asp:TextBox>
<asp:CompareValidator ID="validator" runat="server" ControlToValidate="TextBox"
Operator="DataTypeCheck" Type="Double" ErrorMessage="Value must be a number">
</asp:CompareValidator>
If you use a normal html5 input you can solve your problem very quickly:
<input type="number" id="only-numbers" min="0">
It will also show an error automatically if a wrong value is inserted.
You can use Regular Expression Validator like below.
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Please Enter Only Numbers" ValidationExpression="^\d+$"></asp:RegularExpressionValidator>

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)

asp.net range validator on textbox

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.

Compare fields (validation c#/asp.net)

I'm working on an application with two input fields that's validated in different ways with RequiredFieldValidator, RangeValidator and so on. I need one more validation and that is to check that the number the user writes in input1 isn't bigger than in input2, and here's the question.
Is it possible to use validation controls to compare 2 input fields, or do I need to write code for it? I'm using a ValidationSummary control and of course I want to show all the errors with this. If it isn't possible to use validation controls to compare 2 input fields and I need to write code for this, is it possible to show the error message with the ValidationSummary anyway, and in that case how?
Thanks in advance!
You can use the CompareValidator.
See here for an example.
Use a custom validator control and use the ServerValidate event to return true/false depending on if the check is correct.
The validation summary will pick up that the Page is not valid and display your message.
C#
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (TextBox1.Text.Length > TextBox2.Text.Length)
args.IsValid = false;
else
args.IsValid = true;
}
.aspx
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid Length" Display="None" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
Have you tried using the CompareValidator?
This allows you to compare 2 input fields and is a standard control as per the Requiredfield and Range validators.
<asp:CompareValidator ControlToCompare="text1" ControlToValidate="text2" ErrorMessage="error" runat="server" Operator="LessThan" Type="Integer" />
bool isLonger(string s1, string s2)
{
return s1.Length > s2.Length ? true : false;
}
returns true if the length if s1 is greater than the length of s2

Categories

Resources