Focus remains on textbox on tabbing in Google Chrome browser - c#

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);
}

Related

Not showing asp Label after Visible true

I am taking a value and it is showing correctly (using it as a flag). Now we add a validation, but it does not show if it is meeting the validation. why? I dont know.
.aspx.cs
if (u.StatusRef.ToString().Equals('1'))
{
Email_Sent.Visible = true;
}
.aspx
<asp:Label ID="statusRef" runat="server"></asp:Label> <--- This shows the value '1'
<asp:Label ID="Email_Sent" runat="server" hidden="true">Status: You have notified the candidate to confirm references.</asp:Label>
Thanks,
EB.
You can try to use:
<asp:Label ID="Email_Sent" runat="server" Visible="False">
^^^^^^^^
Instead of hidden="true"

ASP.NET CausesValidation="True" does nothing

I have a form with Required field validator controls to validate if the control is empty or not. I put the CausesValidation="True" in the submit button to prevent the form from saving data if there's required field empty and give an appropriate error message.
However, whenever I click submit, nothing happened: NO error message and the form is not saved either.It just stays there without any event occurring.
Also, i noticed even ALL required fields are correctly filled up, it still does nothing upon submitting, unless if I put the CausesValidation="False", then only I am able to submit the form (but no validations take place, which is not what I want).
here's my code:
My Control to validate:
<asp:TextBox runat="server" ID="TxtCostCenter" Text='<%# Bind("CostCenter") %>' MaxLength="254"
Size="254" SkinID="InputBox" />
<asp:RequiredFieldValidator ID="RfvTxtCostCenter" runat="server" ControlToValidate="TxtCostCenter"
Display="Dynamic" ErrorMessage="<%$ Resources:WebResources, ErrorFieldIsRequired %>" />
also my Submit button (there are two submit buttons though, 1 is at the top and the other is at the bottom of the page):
top:
<asp:LinkButton ID="LinkInsert" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
bottom:
<asp:LinkButton ID="LinkButton2" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
I also tried the ValidationGroup="val" but it still does not work.

Error when using RequiredFieldValidator control for FreeTextBox control

In my aspx page:
...
<tr>
<asp:Label ID="FailureText" runat="server" ForeColor="#CC3300"></asp:Label>
<asp:ValidationSummary ID="Alert" runat="server" CssClass="failureNotification" HeaderText=""/>
<tr/>
<tr>
<FTB:FreeTextBox id="FTB" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FTB"
CssClass="failureNotification" ErrorMessage="Content cannot be empty." ToolTip="Content cannot be empty." ></asp:RequiredFieldValidator>
<tr/>
The first time, the code works fine when I let the FTB empty --> FailureText="Content cannot be empty.";
The 2nd time, I press space to input many spaces in FTB --> FailureText doesnt show and the program does the next codes.
I have used RequiredFieldValidator control before but it worked fine for both null or space value.
Help! I really dont know why the RequiredFieldValidator accept space value here???
Maybe this will help. Its not exactly the same, but it sounds like you need to check if only spaces have been entered.
validation on textbox (no space)
<asp:RegularExpressionValidator ID="rev" runat="server" ControlToValidate="txtBox"
ErrorMessage="Spaces are not allowed!" ValidationExpression="[^\s]+" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
edit...
If you are ok with doing some work server side, this would be an easier solution...
if(string.IsNullOrWhiteSpace(Textbox1.Text))
{
lblError.Text ="Enter required field";
}

Issue related to lost focus in Textbox when RequiredValidator is used

I have an asp.net Web Application, i have login screen in Application.
In userName and password field i have used required field validator as follows :
<td style="width: 160px">
<asp:TextBox ID="txtUserNm" runat="server" CssClass="txtSingleline txtBack-Color txtRequireBorder-Color required" Height="18px" Width="150px" TabIndex="1" MaxLength="50" onblur="ValidatorOnChange(event);showhide();"></asp:TextBox>
</td>
<td style="width: 140px">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
Width="150px" SetFocusOnError="true" ErrorMessage="Please enter Username" ControlToValidate="txtUserNm" ValidationGroup="a"></asp:RequiredFieldValidator>
when i focus on UserName textbox and Press Tab , i am unable to loos the focus from the TextBox because of RequiredField Validator. it happens only in Chrome and IE9 . but working well in firefox.
what can be solution to loose the focus from the Textbox when using Requiredfield validator ???
Thanks
Simple... just remove the SetFocusOnError="true" or set it to false.
The only downside is you can only validate the form on form post back, not on the fly.
But, it's not even such a pain in the neck, since even if you remove this property, validation works fine in both normal web forms, and ajax update panels.
I assume that you should manually assign tabindex property to your controls in order to define loop by pressing tab.

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

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.

Categories