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.
Related
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?
I used asp required field in my code but data in text box are sending even they are empty when clicking on the button and after they are sending, message of required field appears!!
Please help! Thanks in advance...
The source code looks like this
<asp:Label ID="LabelUserName" runat="server" AssociatedControlID="UserName" meta:resourcekey="UserName">Nom :</asp:Label>
<asp:TextBox ID="UserName" runat="server" ValidationGroup="val" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Entrez your name " ControlToValidate="UserName" ForeColor="Red" ValidationGroup="val">
</asp:RequiredFieldValidator>
Here is the code of the button :
<asp:Button runat="server" Text="Envoyer" ID="Button1" Width="78px" ValidationGroup="val" />
Can't say for sure without seeing the code inside the event handle for your button click, but my guess is you are missing a check to be sure the page is valid. For example:
If (Page.IsValid) Then
' do your thing
Else
'do nothing
End If
I think the required will help you to solve this issue of field validator
eg:
<asp:TextBox ID="UserName" runat="server" required="required"></asp:TextBox>
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
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.
Hi I am developing a web page which will give the details of customer. I have several validations for input fields and used DetailsView for displaying details of customer. If the user details does not exist in database Detailsview displays a message "No records found". Now if I enters an invalid user name validations are displayed but page still have the "No records found" message in EmptyDataTemplate of DetailsView. How can I remove "No records found" message or disable details view ? So that page should only show error messages and textbox fields.
<asp:TextBox ID="TextBox_FirstName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator_FirstName" runat="server"
ControlToValidate="TextBox_FirstName" ErrorMessage="Enter a valid SSN" ForeColor="Red"
SetFocusOnError="True">*</asp:RegularExpressionValidator>
<br />
<asp:TextBox ID="TextBox_LastName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator_LastName" runat="server"
ControlToValidate="TextBox_LastName" ErrorMessage="Enter a valid date" ForeColor="Red">*</asp:RegularExpressionValidator>
<asp:Button ID="txtSubmit" Text="Validate" runat="server" />
<asp:CustomValidator ID="AtLeastOneTextBoxValidator" runat="server" ClientValidationFunction="Validate_Textbox"
ValidateEmptyText="true"> </asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary" runat="server" />
<asp:DetailsView ID="Client_DetailsView" runat="server">
<EmptyDataTemplate>
<strong>No Record Found.</strong>
</EmptyDataTemplate>
</asp:DetailsView>
Change your code behind so that Client_DetailsView only gets data bound when they submit the form.
Also, set it's visibility to hidden in the .aspx then only set it to visible when the form is submitted.