comparevalidation for text box value and hidden field value - c#

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

Related

ValidationGroup Not Firing

I have a text box with required field validator:-
<asp:TextBox ID="txtCName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
I need to show error message on button click:-
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
ValidationGroup="sub">
I am using the following code to assign validation group to the required field validator (it is in document.ready() function) :-
$("#<%=rfvCName.ClientID %>").attr('ValidationGroup', 'sub');
But it is not showing any error message on button click. Please help me.
I need to do it using Jquery. I don't want to assign ValidatioGroup directly to the control.
Add validation group ValidationGroup="Sub"
<asp:RequiredFieldValidator ID="rfvCName" runat="server" ErrorMessage="Pleas Enter Name" ValidationGroup="sub" ControlToValidate="txtCName" ></asp:RequiredFieldValidator>
when the page(server side) prerender the object requiredFieldValidator, some javascript is added into page automatically by .net framework.
this javascrript code check the validationgroup.
add manually the validationgroup attribute client-side without this javascript code not work.
you can see this code in developpers tool searching "ValidationGroup"
if you cannot add attribute in html, you can add this attribute in a event server side like load or prerender.

Validation effects the entire form, no button can be pressed

I've got a page that contains a huge amount of controls, (gridviews, reportviewers, buttons etc.)
Now I've a couple of textboxes and dropdowns that represent a "form" user might want to fill up and submit by clicking a button. Some of these controls contain "RequiredFieldValidator"s.
<fieldset runat="server" >
<asp:Label ID="lblName" runat="server" Text="Name:" AssociatedControlID="txtName" />
<asp:RequiredFieldValidator ID="rfvTxtName" runat="server" ControlToValidate="txtName" Text="(Required)" />
<asp:TextBox ID="txtName" Width="175px" runat="server" />
....
....
</fieldset>
The problem I encounter is that no other button on the entire asp form can be clicked because of these validation controls.
The question is; how do I set this validation to only listen to that particular fieldset and not the whole page?
Set the ValidationGroup property of those controls to something unique.
More info here.
Set your controll's ValidationGroup property to make them one group . Like
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
ValidationGroup="MyGroup" />
The ValidationGroup property specifies which group of controls is
validated on validation. This property is mostly used when there are
several buttons in a form.
ASP.NET ValidationGroup Property
You ca also change property CausesValidation to False for control that you don't want to validate.

Change the ControlToValidate of RequiredFieldValidator dynamically

I have a textbox and dropdown inside a <td> based on certain condition I'll have to hide the text box and show the dropdown. If dropdown is visible is it possible to use the same requiredfield Validator. Is this possible?
<td>
<asp:TextBox ID="txtLimit" runat="server" />
<asp:DropDownList ID="ddlCurLiabiltyLimits" runat="server" CssClass="TextNormal" OnSelectedIndexChanged="ddlCurLiabiltyLimits_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtLimit" ErrorMessage="Answer is required." ClientIDMode="Static" />
</td>
In my code behind :
RequiredFieldValidator1.ControlToValidate = ddlCurLiabiltyLimits.ID ;
but this doesn't seem to be working.
One thing that might be causing you problems is when using RequiredFieldValidator on a DropDownList, you will want to specify the RequiredFieldValidator.InitialValue property to indicate the Value of the "first" item in the DropDownList. Maybe that is where your problem is?
My guess is that you might be assigning the ID to the ControlToValidate property too late in the page lifecycle. Is it possible to assign this during the page's OnInit event?
It might be easier to just use a second RequiredFieldValidator for the DropDownList.

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.

How to validate expression on button click in asp.net C#

I am making a website and I have made a form fields like email field and validation expressions associated with it. Validation is initiated on text change. But i want it t execute on "submit" button click event.
I have searched but could not locate the solution to my problem. Please tel me why is this happening and how can i make it right. I am new to this web development field, So need help from you guys.
Thanks in advance!!!
Hamad
You could disable showing errors in the validator itself and instead make a validation summary which will be shown only after you click submit.
Like this:
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ValidationGroup="vRegister" Display="None" ErrorMessage="Email field cannot be empty"></asp:RequiredFieldValidator>
and then declare a validation summary:
<asp:ValidationSummary runat="server" ID="vSummary" ValidationGroup="vRegister" DisplayMode="BulletList" />
What you should do is change the value of EnableClientScript to false. Then call the validation from your code behind (which should always be done since a user can disable their client side validation anyway. Security rule 1, never trust the client)
EnableClientScript: Gets or sets a value indicating whether client-side validation is enabled.
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" EnableClientScript="false"
ErrorMessage="*" ControlToValidate="txtName" ></asp:RequiredFieldValidator>
Code Behind:
protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//Do stuff
}
//No need for else, the validations should display accordingly
}
Additional resources: http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx
Are you simply looking for the validation type controls to validate input?
If so look at the property EnableValidation and set it to true. Doing so will force the validation of the textbox even before the button_Click event can execute.
Try this:
<asp:TextBox ID="txtName" runat="server" Height="23px"
Width="252px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server"
ErrorMessage="*" ControlToValidate="txtName"
ValidationGroup="vadd"></asp:RequiredFieldValidator>
The "Causes Validation" property on the button itself will automatically force your page to meet you validation specifications before firing the rest of the code associated with the button press.
If i have understood your question, you need to create a same validation group for each of the validation control in your aspx page. You also need to have a validation summary with same validation group. And atlast in the submit button in aspx page you have to mention same validation group...

Categories

Resources