Validation called on controls from different validation groups - asp.net

I have 2 buttons and other controls in a page. Button1 has validation group Validation1 and Button2 has Validation2. When I click Button1, controls with Validation2 are validated, which is not supposed to happen. How can I avoid this?

Have you putted validation group to all the controls.You need to give validation groups controls to all the fields including validator controls. Just like following.
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="firstTextBox" ValidationGroup="vg1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="requiredFirst" ControlToValidate="firstTextBox" ValidationGroup="vg1"></asp:RequiredFieldValidator>
<asp:Button runat="server" ValidationGroup="vg1" Text="SaveData" ID="btnSaveFirst"/>
</div>
<div>
<asp:TextBox runat="server" ID="secondTextBox" ValidationGroup="vg2"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="requiredSecond" ControlToValidate="secondTextBox" ValidationGroup="vg2"></asp:RequiredFieldValidator>
<asp:Button runat="server" ValidationGroup="vg2" Text="SaveData" ID="btnSaveSecond"/>
</div>
</form>

Related

Validation Not Firing On TextBox

So, basically I have multiple TextBoxes on my page and some of them have validations with RequiredFieldValidator.
I am using OnTextChanged event on txtMobileNo TextBox Field to fetch the data from database.
If the number exists in the database the the rest of my TextBoxes filled from server side.
Now, I am facing here a problem: my MobileNo Text Field skips the RegularExpressionValidator validation even if it is wrong.
I want to redirect to server side only if my number matches the validation logic.
<div class="col-md-4">
<asp:Label ID="lblfortxtMobileNo" runat="server" Text="Mobile No"></asp:Label>
<asp:TextBox ID="txtMobileNo" runat="server" AutoPostBack="true" OnTextChanged="txtMobileNo_TextChanged" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqfvtxtMobileNo" runat="server" ErrorMessage="*" ControlToValidate="txtMobileNo" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
runat="server"
ErrorMessage="Please Enter Valid Number"
ControlToValidate="txtMobileNo"
Display="Dynamic"
ForeColor="Red"
ValidationExpression="^([0-9]{10})$"
></asp:RegularExpressionValidator>
</div>
<div class="col-md-4">
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server" AutoPostBack="true"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtName" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator><br />
</div>
<div class="col-md-4">
<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />
</div>
When I am set CauseValidation = "true" the result is somewhat what I want .
But it checks all the other RequiredFieldValidator validation as well before calling OnTextChanged function.
I just want to check Regular Expression Validation at this point for txtMobileNo and based on the result wants to display error.
Cross check your regex id it is look like an duplicated id here.

list of all validation errors on page post back including hidden fields in multiview

I am working on an .Net Webforms project and on one page there is a multiview that contains many fields that need validation on each view. All have required field validators, but I need to only validate when the user wants to update the saved data, not when they are just looking at it. Because of this the client side doesn't know of the existence of fields from view1 in the multi view when view 5 is the only one visible. Page.isvalid returns false but I need to display all there errors in a validation summary.
I am not sure what you really want, I think you mean that you want to display all errors from all the "view" in the ValidationSumary?
Some note: using ValidationGroup for all controls, buttons and validationsumary, put validationsumary outside the views...
The best is to show your code here, but here is a sample of working code that may help you, get this from here
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
view 1: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RequiredFieldValidator1"></asp:RequiredFieldValidator>
</asp:View>
<asp:View ID="View2" runat="server">
view 2 :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="RequiredFieldValidator2">*</asp:RequiredFieldValidator>
</asp:View>
</asp:MultiView>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="submit" />
<asp:Button ID="Button2" runat="server" Text="view2" onclick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="view1" onclick="Button3_Click" />

asp:RequiredFieldValidator and radiobuttons

This is my first question in stackoverflow.com.
I am working on a school project which I have to validate the input from the user. Everytime the page load, it give the server error message. Please see the codes, and the error message after.
<div>
<table>
<td>
<asp:RadioButton ID="RadioButton1" runat="server"></asp:RadioButton>
<asp:RequiredFieldValidator ID="validateCheck" runat="server" ControlToValidate="RadioButton1" ErrorMessage="Please Enter" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</table>
</div>
Server Error in '/' Application.
Control 'RadioButton1' referenced by the ControlToValidate property of 'validateCheck' cannot be validated.
The RequiredFieldValidator does not validate a RadioButton. However, you can use the RadioButtonList control instead (validated by RequiredFieldValidator).
The asp:RadioButton do not support validation, instead of RadioButton use a RadioButtonList:'
<form id="form1" runat="server">
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="RadioButtonList1"></asp:RequiredFieldValidator>
</div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
</asp:RadioButtonList>
</form>

How to limit validation to a particular trigger?

Is there any way to limit the validation of a validator control to a particular event or trigger? Say, I want my validator1 to be activated only when button1 was clicked, and validator2 to be activated only when button2 was clicked. How can I do this in asp.net?
ValidationGroups are what you're looking for(ASP.NET >= 2.0)
Validation groups allow you to organize validation controls on a page
as a set. Each validation group can perform validation independently
from other validation groups on the page.
<asp:textbox id="AgeTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1"
controltovalidate="AgeTextBox"
validationgroup="PersonalInfoGroup"
errormessage="Enter your age."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button1 is clicked, only validation
controls that are a part of PersonalInfoGroup
are validated.-->
<asp:button id="Button1"
text="Validate"
causesvalidation="true"
validationgroup="PersonalInfoGroup"
runat="Server" />
<br /><br />
<asp:textbox id="CityTextBox"
runat="Server">
</asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator2"
controltovalidate="CityTextBox"
validationgroup="LocationInfoGroup"
errormessage="Enter a city name."
runat="Server">
</asp:requiredfieldvalidator>
<br /><br />
<!--When Button2 is clicked, only validation
controls that are a part of LocationInfoGroup
are validated.-->
<asp:button id="Button2"
text="Validate"
causesvalidation="true"
validationgroup="LocationInfoGroup"
runat="Server" />

ASP.NET Validator doesn't fire

I've a problem with a RequiredFieldValidator on a page with two Panels, which represent two different views, i.e. only one panel is visible at one time, but they share same submit button.
By default the "BatchReturnPanel" is visible and "SingleReturnPanel" is hidden, and in this case all validators on page work just fine. But if I hide the "batch.." panel and show "single.." panel (ChangeViewButton click event) then the requieredFieldValidator "DropDownListFieldValidator" doesn't fire up, and page is always considered validated. Why does this happen and how can this problem be solved? Thanks for help/
Below is extract from the ASPX page:
<div>
<asp:DropDownList ID="MDDropDownList" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator id="DropDownListFieldValidator" runat="server" ControlToValidate="MDDropDownList"
InitialValue="-" ErrorMessage="Please select Master Distributor" EnableClientScript="true" CssClass="error"
Display="Static" ValidationGroup="Voucher" />
<asp:Panel runat="server" ID="BatchReturnPanel">
<p>
<asp:TextBox ID="FirstVoucherTextBox" runat="server" CssClass="Scanner RRS"></asp:TextBox>
<asp:RequiredFieldValidator ID="FirstVoucherFieldValidator" runat="server" ControlToValidate="FirstVoucherTextBox" ValidationGroup="Voucher"
ErrorMessage="Valid Voucher ID for the first voucher in batch is required<br>" Display="Dynamic" CssClass="error" EnableClientScript="false"></asp:RequiredFieldValidator>
</p>
</asp:Panel>
<asp:Panel runat="server" ID="SingleReturnPanel" Visible="false">
<p>
<asp:TextBox ID="SingleVoucherTextBox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="SingleVoucherTextBox2" runat="server"></asp:TextBox><br />
<asp:TextBox ID="SingleVoucherTextBox3" runat="server"></asp:TextBox><br />
</p>
</asp:Panel>
<asp:Button ID="VoucherSubmitButton" runat="server" Text="Enter"
OnClick="VoucherSubmitButton_Click" UseSubmitBehavior="false" ValidationGroup="Voucher"/>
</div>
<asp:Panel CssClass="grid_4 box" runat="server" ID="ChangeViewPanel">
<asp:LinkButton runat="server" ID="ChangeViewButton"
onclick="ChangeViewButton_Click">Single item return mode</asp:LinkButton>
</asp:Panel>
You can use validation group and client side page validation.
function Validation() {
Page_ClientValidate('validationGroupName');
if (!Page_IsValid) {
return false;
}
else {
return true;
}
}
Hope this helps

Resources