RangeControlValidator Misbehaving - c#

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)

Related

HTML Required field not working as needed

In am using htm required="required" validation but it is not working properly as needed.
I have 2 button on single page and I want that on 1 button click it validate for textbox1 and on click of 2nd button it will validate for textbox2.
Please review attached image for more detail
You are looking for ValidationGroup. By giving a collection of Validators and a Button the same id, the corresponding button will only validate those fields.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button Group 1" ValidationGroup="group1" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:Button ID="Button2" runat="server" Text="Button Group 2" ValidationGroup="group2" />
I don't think HTML does that natively (grouped validation).
If server-side is not an option, then you'll need to use some JavaScript.
I have found some inspiration in this answer for a related question (Triggering HTML5 Form Validation) : https://stackoverflow.com/a/39689115/370786
If you group each set of inputs that you want to validate together in a form (so you will have several forms), then you can trigger validation through Javascript. Then you can stop the form from posting. This can be done through Javascript - maybe it can be done in HTML but I'm not sure.
Then once everything has validated you will need to post the data to the server, using some Javascript. It might be as simple as just triggering the submit() method on a form element.

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>

ASP Validators not working?

I feel embarrassed to be posting this as I'm pretty sure I'm missing something really simple, and this certainly isn't the first time I've used asp validators. I have the following validator controls:
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtTitle" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAbbr" EnableClientScript="false" ErrorMessage="*" ValidationGroup="groupUpdateAL" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ddlColour" EnableClientScript="false" ValidationGroup="groupUpdateAL" ErrorMessage="*" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="groupUpdateAL" CausesValidation="true" /><br />
For some reason, if leaving the controls empty I am given the following error:
Length cannot be less than zero.
Parameter name: length
It seems the validation controls aren't catching an invalid input.
What I've Tried:
I suspected it may have been client validation intercepting the postback, which is why you can see EnableClientScript is set to false now. Didn't work.
Try testing page validity in the onclick event of your btnSave :
if(Page.IsValid)
{
//Do your stuff
}
else
{
Response.Write("error");
}
Validation is on new screen
in c# file avalilabel on this image plzz click
on this image
enter image description here
Create database to get querystring
Create connection file in visual studio

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.

ValidationSummary not working when ValidationGroup is specified

I have a few section in an ASP.NET page and need to validate them separately.
Each section has it's own validation summary section so I thought of using the ValidationSummary tag with the ValidationGroup attribute but it does not work.
The following code works but validates all controls in the page:
<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" />
<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" />
....
<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" />
While the following does not work (no validation whatsoever, on submit I just go to the next page in the wizard):
<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" ValidationGroup="xxxx" />
<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" ValidationGroup="xxxx" />
....
<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" ValidationGroup="xxxx" />
What am I missing here? Is there extra setup needed or something else?
The default behaviour in ASP.NET is that when the user clicks a button that has no ValidationGroup specified (and has CausesValidation set to true), all validation controls that do not belong to a validation group are validated.
Therefore, to validate a specific group, you need to set the ValidationGroup property of the button that should cause the validation (and also possibly the CausesValidation property).
See the MSDN documentation for Button.ValidationGroup for details and an example.
EDIT: If you need to validate ALL groups on the page, the easiest way is of course to not use validation groups at all. If you however want to validate only some (but more than one) groups, you can do it on the server (in the click handler of the button) by calling:
Validate("groupOne");
Validate("groupTwo");
// ...
Note that this won't trigger client-side validation. See for example this post for a discussion about triggering multiple validation groups on a single button click.
EDIT: I found a blog post describing how to build a reusable "multiple validation group button" for ASP.NET, complete with code. Haven't tried it, but it looks promising.

Categories

Resources