Field validation as required only if true - asp.net

I have such a Checkbox and DropDownList with Validator
<asp:CheckBox ID="chkIncludeLogin" runat="server" Text='MyCheckBox' Font-Size="9pt"/>
<asp:DropDownList ID="ddlLanguage" runat="server" ViewStateMode="Enabled" ></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldLanguage" runat="server" ControlToValidate="ddlLanguage"
InitialValue="-1" SetFocusOnError="True" ForeColor="Red" ValidationGroup="check"
Display="Dynamic" Text='*'></asp:RequiredFieldValidator>
I have this problem:
When I do not check CheckBox and do further actions, Validator does not start and everything works fine
When I check CheckBox and uncheck it, Validator works and does not allow to perform other actions.
How to make the validator not take this field into account when I check and uncheck the CheckBox?

Related

validate =false not validating on button click

I have 2 textboxes called minimum version and maximum version requesting user to enter Version ( like 1.3.4 ). At a given time only one can be blank, not both So I have provided custom field validator .Also I have provided regular expression to validate if values entered are proper.
When I click on button entering invalid version field I can see button click event is getting triggered. Could you please help.
<asp:Label ID="lblMinimum" runat="server" Text="Minimum Version : " CssClass="alignLeft"></asp:Label>
<asp:TextBox ID="txtMinimumVersion" runat="server" Width="312px" CssClass="alignRight" ValidationGroup="validate"></asp:TextBox>
<asp:RegularExpressionValidator ID="revModuleVersion" runat="server" ControlToValidate="txtMinimumVersion"
ValidationExpression="^\d+(\.\d+)+$" ErrorMessage="* Please enter valid version number" ValidationGroup="validate"/>
<asp:Label ID="lblMaximum" runat="server" Text="Maximum Version : " CssClass="alignLeft"></asp:Label>
<asp:TextBox ID="txtMaximumVersion" runat="server" Width="312px" CssClass="alignRight" ValidationGroup="validate"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtMaximumVersion"
ValidationExpression="^\d+(\.\d+)+$" ErrorMessage="* Please enter valid version number" ValidationGroup="validate"/>
<asp:Button ID="btnUpdateNow" runat="server" EnableViewState="False" Text="Create now"
OnClick="btnCreateNow_Click" CausesValidation="false" ValidationGroup="validate"></asp:Button>
<asp:CustomValidator ID="custValidateModuleVersion" runat="server" Display="Dynamic" ErrorMessage="* Maximum version should be greather or equal to Minimum version"
OnServerValidate="custValidateModuleVersion_ServerValidate"></asp:CustomValidator>
<asp:CustomValidator ID="custValidate" runat="server" Display="Dynamic" ErrorMessage="* Please enter atleast one version"
OnServerValidate="CustomValidator_ServerValidate"></asp:CustomValidator>
You have mentioned CausesValidation = "false". Make it as true or remove it
<asp:Button ID="btnUpdateNow" runat="server" EnableViewState="False" Text="Create now" OnClick="btnCreateNow_Click" ValidationGroup="validate"></asp:Button>
The CausesValidation property specifies if a page is validated when a Button control is clicked.
Page validation is performed when a button is clicked by default.
This property is mostly used to prevent validation when a cancel or reset button is clicked.

RegularExpressionValidator in Asp.net

I have search textbox,which has default value Enter Month to View Profit. When I click search button without entering any data, the default value of textbox is posted to server for search. I want that RegularExpressionValidator do not validate default value of textbox.
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!"
>
</asp:RegularExpressionValidator>
The default for all validators except the RequiredFieldValidator control if you post with empty field the validator will not trigger
you must use required field validator with other validators to prevent the postback to happen
from MSDN
Special-Case Validation Results for ASP.NET Server Controls
EDIT
Also if you add your controls as in your question it should work but if there is other controls like for example other button you should set the validationGroup Property to the group that you want to work together
ValidationGroup="vGrp"
and your code will be like this
<asp:TextBox ID="Tboxsearch" Text="Enter Month to View Profit" OnClick="this.value=''" CssClass="textboxinput" runat="server" ValidationGroup="vGrp"></asp:TextBox>
<asp:Button ID="ButtonSearch" CssClass="btnLog" runat="server" Text="Search" onclick="ButtonSearch_Click" ValidationGroup="vGrp" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidatorname"
runat="server"
ControlToValidate="Tboxsearch"
ForeColor="Red"
Text="*"
ValidationGroup="vGrp">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidatorname"
runat="server"
ValidationExpression="[a-zA-Z0-9]+"
ForeColor="Red"
ControlToValidate="Tboxsearch"
ErrorMessage="Enter Valid Name!" ValidationGroup="vGrp"></asp:RegularExpressionValidator>
from MSDN about validationGroup

Prevent textbox from firing validators

So I have a textbox with 6 validators. 2 of each kind because I have two validation groups depending on what button the user clicks. My problem is everytime the textbox loses focus two error messages get displayed, and this looks wierd. I've tried setting the CausesValidation property of the textbox to "false" but it's not working.
Textbox & Validators -
<asp:TextBox ID="collectionDtl_Qty" runat="server" CssClass="formText"
AutoPostBack="false" CausesValidation="false" Text="0">
</asp:TextBox>
<asp:FilteredTextBoxExtender ID="collectionDtl_Qty_Filtered" runat="server"
FilterMode="ValidChars" TargetControlID="collectionDtl_Qty" ValidChars="1234567890,">
</asp:FilteredTextBoxExtender>
<asp:CustomValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_CValidator"
runat="server" ClientValidationFunction="coll_QtyValidator"
Text="Quantity cannot be greater than requested quantity." ForeColor="Red"
ValidationGroup="formValidation" Display="Dynamic">
</asp:CustomValidator>
<asp:CustomValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_CValidator2"
runat="server" ClientValidationFunction="coll_QtyValidator"
Text="Quantity cannot be greater than requested quantity." ForeColor="Red"
ValidationGroup="detailValidation" Display="Dynamic">
</asp:CustomValidator>
<asp:CompareValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_Comparer"
runat="server" Text="Quantity must be greater than 0." ForeColor="Red"
ValidationGroup="formValidation" Display="Dynamic"
ValueToCompare="0" Operator="GreaterThan" Type="Integer">
</asp:CompareValidator>
<asp:CompareValidator ControlToValidate="collectionDtl_Qty" ID="collectionDtl_Qty_Comparer2"
runat="server" Text="Quantity must be greater than 0." ForeColor="Red"
ValidationGroup="detailValidation" Display="Dynamic"
ValueToCompare="0" Operator="GreaterThan" Type="Integer">
</asp:CompareValidator>
Any help would be appreciated.
Hi according to your question.what i understand you have to disabled validator or use validation based on Button click.
For Button click use ValidationGroup
ValidationGroup="save"
If you want to disabled validators on condition .Try this one
ValidatorEnable(document.getElementById('<%= rqrgvddlCategory.ClientID %>'), false);
Hope it will helps you.

Required Field Validator gets run on post back trigger

I have a dropdown list on my aspx page on which I have a RequiredFieldValidator applied over it. DropDown code is:
<asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2"
AutoPostBack="true" ValidationGroup="Save" DataSourceID="dtsglCategoryId" DataTextField="LookupItem"
DataValueField="Id" AppendDataBoundItems="true">
<asp:ListItem Text="All" Selected="True" Value="0"></asp:ListItem>
</asp:DropDownList>
RequiredFieldValidator code is:
<asp:RequiredFieldValidator ID="rfvddlglCategoryId" InitialValue="0" runat="server"
ErrorMessage="Please select category" CssClass="Validations" ControlToValidate="ddlglCategoryId"
ValidationGroup="Save" Display="Dynamic" SetFocusOnError="true">
</asp:RequiredFieldValidator>
I have a post back trigger as well on my aspx page and when I change the category from the dropdown, page posts back and a grid on my page gets updated. But due to post back, the validator message appears and then disappears. I want this to appear only when "All" is selected from dropdown and user clicks save button.
Any guidelines?
Trigger:
</ContentTemplate>
<Triggers>
<%--<asp:AsyncPostBackTrigger ControlID="lbFileName" />--%>
<asp:PostBackTrigger ControlID="btnFileUploadSave" />
</Triggers>
</asp:UpdatePanel>
Button:
<asp:ImageButton ID="btnFileUploadSave" runat="server" ValidationGroup="Save"
ImageUrl="~/App_Themes/Default/images/update.png" ToolTip="Save"
Height="18px" onclick="btnFileUploadSave_Click"/>
You may need to set the AutoPostBack property of the dropdownlist to false. It sounds like a postback is getting fired with the OnSelectedIndexChanged event handler.
Set the ValidationGroup Property Of all Validaters same which you want to validate on button click and then set buttons ValidationGroup property same as Your Validators
For Example:
<asp:RequiredFieldValidator ID="rfvDoc" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Please Select A Document" ValidationGroup="OnSave">
</asp:RequiredFieldValidator>

Dropdownlist validation in Asp.net Using Required field validator

I have Dropdownlist whose value field and text field are bind at runtime.
it has --select-- as first item with a value of 0
and the rest of the values are bind at runtime.
I have given validaton group for both the control and the validator as "g1"
and Intialvalue=0
But still the page is posting back even if I select --select-- option.
<asp:DropDownList AutoPostBack="true" CssClass="dropdown" ValidationGroup="g1"
ID="ddlReportType" runat="server"
OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddlReportType" ID="RequiredFieldValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
InitialValue="0" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
And code Behind to Bind the Dropdown
ddlReportType.Items.Clear();
ddlReportType.DataSource = dt.Tables[0];
ddlReportType.DataTextField = "ReportType";
ddlReportType.DataValueField = "ReportTypeID";
ddlReportType.DataBind();
ddlReportType.Items.Insert(0, new ListItem("--Select--", "0"));
//ddlReportType.Items[0].Value = "0";
ddlReportType.SelectedIndex = 0;
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"
ValidationGroup="g1" runat="server" ControlToValidate="ControlID"
Text="*" ErrorMessage="ErrorMessage"></asp:RequiredFieldValidator>
Here use asp:CompareValidator, and compare the value to "select" option.
Use Operator="NotEqual" ValueToCompare="0" to prevent the user from submitting the "select".
<asp:CompareValidator ControlToValidate="ddlReportType" ID="CompareValidator1"
ValidationGroup="g1" CssClass="errormesg" ErrorMessage="Please select a type"
runat="server" Display="Dynamic"
Operator="NotEqual" ValueToCompare="0" Type="Integer" />
When you do above, if you select the "select " option from dropdown it will show the ErrorMessage.
I was struggling with this for a few days until I chanced on the issue when I had to build a new Dropdown. I had several DropDownList controls and attempted to get validation working with no luck. One was databound and the other was filled from the aspx page. I needed to drop the databound one and add a second manual list. In my case Validators failed if you built a dropdown like this and looked at any value (0 or -1) for either a required or compare validator:
<asp:DropDownList ID="DDL_Reason" CssClass="inputDropDown" runat="server">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Expired</asp:ListItem>
<asp:ListItem>Lost/Stolen</asp:ListItem>
<asp:ListItem>Location Change</asp:ListItem>
</asp:DropDownList>
However adding the InitialValue like this worked instantly for a compare Validator.
<asp:ListItem Text="-- Select --" Value="-1"></asp:ListItem>
Add InitialValue="0" in Required field validator tag
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID"
Display="Dynamic" ValidationGroup="g1" runat="server"
ControlToValidate="ControlID"
InitialValue="0" ErrorMessage="ErrorMessage">
</asp:RequiredFieldValidator>

Resources