ASP.NET CasusesValidation="false" not working on link button in IE - c#

I have an issue with causes validation on my link button in my aspx page. What I have is a page with two text boxes, some validation controls and a submit button. The validation controls have enable client script set to true, and have validation groups. My asp image button has a validation group assigned and causes validation equal to true. I have the focus set to my first text box.
My link button is a click here to learn more about..... and I've specified the validation group and set causes validation to false. In Chrome, everything works fine. In IE, when either clicking any where on the page besides the submit button, or when clicking my link button, my first text box is validated and the error is presented. Now, if I click on the link button again, I am redirected to the page I desire.
This seems a bit weird to me, any one have any ideas?
So, how do I fix this, so IE won't validate until only the submit button is clicked.
Here is what my code looks like:
<span>Value 1:</span>
<asp:TextBox ID="Value1TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value1_1_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
<asp:RegularExpressionValidator ID="Value1_2_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" ValidationExpression="\somevalexpression\" Display="Dynamic" ForeColor="Red" Text="is not valid" ValidationGroup="MyValGroup" runat="server" />
...
<span>Value 2:</span>
<asp:TextBox ID="Value2TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value2_1_Validator" EnableClientScript="true" ControlToValidate="Value2TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
...
...
<asp:ImageButton ID="SubmitButton" ImageUrl="~/Images/submit.png" OnClick="SubmitButton_Click" ValidationGroup="MyValGroup" CausesValidation="true" runat="server" />
...
<asp:LinkButton ID="OurPolicyLink" PostBackUrl="~/Policy.aspx" ValidationGroup="MyValGroup" CausesValidation="false" Text="click here" runat="server" />
I came up with a fix using jquery and javascript, its not as clean, but it does work. I would have thought just doing causes validation equal to false would be enough. Perhaps I was wrong.
I have a script javascript file, here is what is looks like.
$(function () {
$"[id$=_Value1TextBox]").focus();
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = false;
});
$("[id$=_SubmitButton]").click(function () {
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = true;
});
Page_ClientValidate("MyValGroup");
if (!Page_IsValid) {
return false;
}
return true;
});
});

It is because you are setting the focus on the text box. When the focus leaves the text box it is triggering validation. Either don't set the focus on the text box or set CuasesValidation=False on the TextBox. The problem with latter option is that it won't validate the text box until something else in the same validation group triggers the validation.

Related

Submit form after asp:RegularExpressionValidator check form valid?

I have a form with this markup:
<asp:TextBox ID="txtMeMail" runat="server" Width="250px" ToolTip="error"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="mandatory msg" ID="RegularExpressionValidator1" runat="server" Display="Dynamic" ValidationGroup="validEmail" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtMeMail" ErrorMessage="error" EnableClientScript="true" />
<button class="button noMarginLeft" runat="server" validationgroup="validEmail" accesskey="S" id="btnSave" onserverclick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label">
<asp:Label runat="server" Text="label"></asp:Label>
</button>
When I input "abc" and remove the focus from the text box, the invalid email message shows up. After that, I correct the text box with a valid email and I keep the focus on the text box, then I click the submit button. The validation message disappears but the form does not submit.
Is there any way to validate and then submit the form?
Try using
<asp:Button class="button noMarginLeft" runat="server" ValidationGroup="validEmail" accesskey="S" ID="btnSave" OnServerClick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label" />
The problem is that you use the ordinary html Button tag. Although it can work it is not recommended because you lose functionality, see How can I use the button tag with ASP.NET?
If you do want content inside the button then use the LinkButton.

Tries to validate when pressing logout button instead of submit

On my admin pages I've got a "Log out" button. On one page there's this form you can fill in and submit and it has some validator controls. The problem is that when I am on this page, I can't log out, because it wants the textboxes to be filled in, even though it's - obviously - not submitted via the log out button. Are the validators executing everytime you try to leave this page, even though I'm not trying to submit a form? To be clear: it works for every other page, it's just the validation here that stops it.
Form code:
<p>
<asp:Label ID="lblA" runat="server" Text="LabelA"></asp:Label><br />
<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvA"
runat="server"
ControlToValidate="txtA"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="lblB" runat="server" Text="LabelB"></asp:Label><br />
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvB"
runat="server"
ControlToValidate="txtB"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
Logout button:
<asp:Button runat="server" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
When Log out is clicked:
protected void btnLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
Any ideas?
add cause validation false to your button
<asp:Button runat="server" CausesValidation="False" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
Also you can use the ValidationGroup property of the controls.
Assign the same ValidationGroup to the group of controls(and RequiredFieldValidator) which you require for validation like username and password text boxes and login button(In your case to the two text boxes) and assign the different ValidationGroup to the other controls(In your case to logout button).
So the validation fires only when the same validation group's button has been clicked.

ASP.NET - Validating Form Before Standard Validation

I have an ASP.NET form where I have an ImageButton control as follows (please note that this only part of a bigger form):
<asp:CheckBox runat="server" ID="checkbox1" Text="Validate" Enabled="false" />
<asp:TextBox runat="server" ID="textbox1" MaxLength="30" />
<asp:RequiredFieldValidator ID="validator1" ControlToValidate="textbox1" Text="required!" runat="server" ValidationGroup="group1" />
<asp:ImageButton ID="button1" runat="server" Width="24" ValidationGroup="group1" />
Using Javascript, I could disable and enable the RequiredFieldValidator as follows:
function test() {
var checkbox1 = document.getElementById("<%=checkbox1.ClientID %>");
var ckOwnerIsOperator = document.getElementById('<%=ckOwnerIsOperator.ClientID %>');
if (checkbox1.checked)
ValidatorEnable(validator1, false);
}
What I am looking for is an event that fires before the validation on the button when the button is clicked. I know I could do this on checkbox click event, but it is not what I need. The checkbox click event has a problem where I lose the validator state if another control triggered an async postback, so I want the test() function to be called before the page validation occurs.
Thank you all
You could add onclientclick="test()" to the button.

Focus remains on textbox on tabbing in Google Chrome browser

I am faced with this peculiar problem that happens only for the Google Chrome browser.
For any textbox which has a required field validator, if I enter some values and tab out of it, it works fine.
However, if the value is removed (the content is totally deleted) and tab out of the textbox, or click on another textbox, for the first time the focus remains on the textbox.
How can I solve this issue? It works fine on all other browsers.
An example of a textbox would be:-
<asp:Label ID="Label6" runat="server" AssociatedControlID="SwiftCode" meta:resourcekey="SwiftCodeLabel"/><span class="required">*</span>
<asp:TextBox id="SwiftCode" runat="server" CssClass="field-input account-detail"/>
<asp:RequiredFieldValidator runat="server" EnableClientScript="True" ValidationGroup="BankDetails" ControlToValidate="SwiftCode" ID="SwiftCodeValidator" SetFocusOnError="True" meta:resourcekey="SwiftOrRoutingValidator" CssClass="validation-always-hide" />
<asp:CustomValidator runat="server" ValidationGroup="BankDetails" EnableClientScript="True" ControlToValidate="SwiftCode" ID="SwiftCodeCustomValidator" ClientValidationFunction="bankAccount.isSwiftCodeValid" SetFocusOnError="True" meta:resourcekey="SwiftCodeCustomValidator" CssClass="validation-always-hide" />
<span class="hint bank-details-hint"><%=GetLocalResourceString("SWIFTBIC")%> <%=GetLocalResourceString("SWIFTBICHint")%></span>
Now the bankAccount.isSwiftCodeValid is a simple jQuery function that goes like this:-
function isSwiftCodeValid(source, args) {
var isAlphaNumeric = /^[a-z0-9]+$/i;
args.IsValid = (args.Value.length === 8 || args.Value.length === 11) && isAlphaNumeric.exec(args.Value);
}

Validate dropdown from custom control in page

I have a custom control included in a form that includes a dropdown list. The form has a number of other required fields, so i was wondering how to validate this dropdown.
<gaia:TextBox ID="TitleTextBox" runat="server"/>
<gaia:RequiredFieldValidator runat="server" ControlToValidate="TitleTextBox"
ErrorMessage="Please fill in the press release title" Text="*" Display="None" ValidationGroup="save" />
<CN:ProductCategoryDropDown runat="server" ID="ProductCategoryDropDown" />
<gaia:CustomValidator runat="server" ID="ProductCategoryValidator" OnServerValidate="ProductCategory_Validate" ValidationGroup="save"
Display="None" Text="*" ErrorMessage="Please select a category" />
the code behind looks like this
protected void ProductCategory_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = (ProductCategoryDropDown.SelectedValue>0);
}
On the customvalidator above, I purposely left out the 'ControlToValidate' because it throws an error.
Please help.
The easiest would be to include the CustomValidator in the UserControl.
Then you could provide a property for the Validation-Group and another ValidatorEnabled to set the validator group and enable/disable the validator.

Categories