I have a custom validator attached to a textbox control as follows
<td align="center" width="10px">
<asp:CustomValidator ID="validateDateText" ControlToValidate="dateTextBox"
runat="server" OnServerValidate="ValidateDate"
ClientValidationFunction="Validate_Date" EnableClientScript="true"
Width="10px" CssClass="errortext" Text="*" Font-Size="Medium" Font-Bold="true" />
</td>
<td align="center" width="80px">
<asp:Textbox ID="dateTextBox" MaxLength="100" runat="server"
CssClass="dateselectortextbox" style="margin-right: 3px;" />
</td>
When I click a button on the page with causesvalidation="true" the client script fires and the validation summary reflects the error message and the validator shows the *
However when I click out of the textbox only the * is displayed by the validator the validationsummary is not updated
The client side validation is working as the serverside code does not get called im just trying to work out why the validationsummary does not get updated on the onblur event
Any ideas?
EDIT:
The ErrorMessage is set in the codebehind for the validator
I have added the EnableClientScript to my validationsummary
I have added ValidationGroup to my validationsummary, customvalidator, textbox and button, but still the validation summary updates for the button click but not the textbox onblur event
You definitely need to use the errormessage="xyz" for the message to show in the validation summary. The validation group shouldn't matter unless you have more than one group of controls you're validating.
Here is a link to another post that may help you with getting the validation summary to update after the onblur.
I think you might want to use ErrorMessage ="Some informative error message" inside your CustomValidator. You also need set ValidationGroup ="SomeGroupName" for CustomValidator ,ValidationSummary control and also the control which is causing postback.
You need to set dateTextBox.ValidationGroup, validateDateText.ValidationGroup and yourValidationSummary.ValidationGroup to the same value.
See http://msmvps.com/blogs/brianmadsen/pages/ASP.Net-2.0-_2D00_-How-to-use-the-new-validation-features_2C00_-part-1.aspx.
Related
I have put server side validations for each text box using <asp:RequiredFieldValidator/>. I have called ClearFields() method on page load that will clear all fields on the form when the button is clicked. The problem is that when the form gets posted and the fields are cleared, the validation message appears again. How to hide the validation messages on form post back. I am sorry, but its been years I have not coded in aspx and I can't find any solution online.
This is the textbox code:
<asp:TextBox runat="server" CssClass="form-control" placeholder="Your Name *" ID="name"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="name" ErrorMessage="Name seems empty" CssClass="help-block text-danger"></asp:RequiredFieldValidator>
This is the button code:
<asp:Button runat="server" class="btn btn-xl" Text="Send Message" ID="submit" OnClick="submit_Click" CausesValidation="false"/>
I guess you are getting the validation message due to button used for clearing the fields. Set CausesValidation property of button to false:-
<asp:Button ID="ClearButton" runat="server" CausesValidation="false" Text="Clear" />
Also, please note asp:RequiredFieldValidator works on client side i.e on the browser it is not a server side validation.
Update:
Since you are clearing your fields on click of button, you can clear in submit_Click method itself after sending the email instead of page load. Ideally you should have a separate button to clear the form though.
I have more than one RequiredFieldvalidator in my markup, I want to check that all fields are entered, and if not I want to show an alert saying so.
Try ValidationSummary control
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
http://asp-net-example.blogspot.nl/2008/10/validationsummary-example-how-to-use.html
I am having trouble with the <asp:RequiredFieldValidator> in this code.
<asp:Label ID="email_Label" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="email_Text" runat="server" MaxLength="40" Width="250"></asp:TextBox> *
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" Text="Required"></asp:RequiredFieldValidator>
According to the W3schools documentation here I have used the <asp:RequiredFieldValidator> correctly, however instead of displaying the label and text box the page also displays the error message. This happens on page_load so the value has not had a chance to change from the default yet. I want the error text to display after the user clicks the Save button at the bottom of the form I am building.
What is Being Displayed:
Email[TextBox] * Required
What Should Be displayed:
Email[TextBox] *
Am I missing a parent element for the validator or something. According to the example on w3schools site no parent element should be needed. In fact the way they have their example set-up is exactly what I was expecting for this.
Use Validation Group if want to display Error message on Button click. Like this.
<asp:RequiredFieldValidator ID="rqtxtQName" ValidationGroup="save" ControlToValidate="txtQueueName" runat="server" ErrorMessage="Some required field are missing." SetFocusOnError="True"Display="Dynamic"></asp:RequiredFieldValidator>
and use validation group on Button also.
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="save" OnClick="btnSubmit_Click"/>
Hope it will helps.
you should use ErrorMessage instead of Text
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="email_Text" ErrorMessage="Required"></asp:RequiredFieldValidator>
If you want to validation on save button click event then set property ValidationGroup="Group1"
for RequiredFieldValidator and also for save button. So it will check validation when you click save button.
And For display message you can use ErrorMessage property.
Thanks,
Hitesh
Set the RequiredFieldValidator this way. Remove the property: 'TextRequired' & put a * inbetween the opening and closing tag.
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="email_Text">*</asp:RequiredFieldValidator>
Also, Do not set the ErrorMessage property as this will again not fit your requirements.
We are developing a web application in Umbraco 4 and have come across an intermittent problem when posting data between pages. When a form submission is posted to a new page all of the validators are firing causing various unwanted results, we have tried various posting methods using different buttons with the same result. Has anyone else come across this issue?
The validation group is working but I got to the bottom of the issue.
One of the submit buttons on the page was a html submit, with no runat server, so the code-behind didn't know where the submit had come from, so all validations where fired, regardless of validation group
All postbacks events, unless otherwise specified, will cause all validators on a page to be triggered. Either...
A) Set the ValidationGroup property on each of the validators as well as the control which you do want to trigger validators to the same name.
<asp:RequiredFieldValidator ID="valName" runat="server" AssociatedControlID="txtName" ValidationGroup="AllRequired" />
<asp:TextBox ID="txtName" runat="server" />
<asp:Button ID="btnSubmit" runat="server" ValidationGroup="AllRequired" OnClick="..." />
<asp:Button ID="btnSkip" runat="server" OnClick="..." />
In this case, btnSkip won't trigger the validators.
B) Set the CausesValidation property on the control which you don't want to trigger validation to false.
<asp:Button ID="btnSubmit" runat="server" CausesValidation="false" OnClick="..." />
I have two validation groups on a form (we will call them VG1 & VG2). I have the following code:
<asp:TextBox ID="textbox1" runat="server" ValidationGroup="VG2" />
<asp:RequiredFieldValidator Text="*" ForeColor="#C301B9" ID="RequiredFieldValidator1" runat="server" ErrorMessage="My error message" ControlToValidate="textbox1" ValidationGroup="VG2" />
When this control has focus and I hit enter the validation summary displays the validation error messages for VG1.
VG1 fields are not visible (set via JS). I think I may need to also disable VG1 validation group summary.
Any help with this would be appreciated.
Hitting enter when in the text box is probably submitting the form or doing the equivelent of clicking some button that is in the VG1 validation group. If you put everything in VG2 within a Panel control and set the DefaultButton property on the panel to be a button in VG2 then hitting enter in the text box will no longer fire the validators in VG1. If you are simply hiding controls vai javascript remember they are still there on the page and their actions can still be triggered.