ASP Required Field Validator shows asterisk instead of Error Message - c#

I've done some searching around and seem to be the only one with this sort of issue. I have a few asp:RequiredFieldValidator on my form and basically what happens is that when the validation is tripped, only a red asterisk shows up and not my error message.
Here is the validator:
<asp:RequiredFieldValidator runat="server" CssClass="label label-danger"
ControlToValidate="email" ErrorMessage="Please provide an email address."
Display="Dynamic" SetFocusOnError="true" Text="Please provide an email address." />
And here is the control being validated:
<asp:TextBox runat="server" CssClass="form-control" ID="email" placeholder="First.Last" ClientIDMode="Static" TabIndex="1"></asp:TextBox><span class="input-group-addon">#ourdomain.com</span>
All of this is inside a master page and an update panel.

To show only the asterisk instead of Error Message type the asterisk into the tag
<asp:RequiredFieldValidator runat="server" CssClass="label label-danger"
ControlToValidate="email" ErrorMessage="Please provide an email address."
Display="Dynamic" SetFocusOnError="true" Text="Please provide an email address.">*</asp:RequiredFieldValidator>
The span will display * on the validator and the validator summary will show the complete message

Related

Regular Expression Validator does not validate until after the form is submitted

<asp:TextBox ID="Email" runat="server" ValidationGroup="register" Placeholder="Email" Width="250px" CausesValidation="true" required="true" MaxLength="200"></asp:TextBox>
<asp:RegularExpressionValidator ID="revEmailAddress" runat="server" SetFocusOnError="true" Display="Dynamic" ControlToValidate="Email" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="register" ForeColor="Red">Please enter a valid email address.</asp:RegularExpressionValidator>
If I test the above code in visual studio the validator works perfectly, but once I publish the project the validation only occurs after I submit my form which is useless. Why won't the validation work after publishing?

HTML Required field not working as needed

In am using htm required="required" validation but it is not working properly as needed.
I have 2 button on single page and I want that on 1 button click it validate for textbox1 and on click of 2nd button it will validate for textbox2.
Please review attached image for more detail
You are looking for ValidationGroup. By giving a collection of Validators and a Button the same id, the corresponding button will only validate those fields.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox2" ValidationGroup="group1"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button Group 1" ValidationGroup="group1" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox3" ValidationGroup="group2"></asp:RequiredFieldValidator>
<asp:Button ID="Button2" runat="server" Text="Button Group 2" ValidationGroup="group2" />
I don't think HTML does that natively (grouped validation).
If server-side is not an option, then you'll need to use some JavaScript.
I have found some inspiration in this answer for a related question (Triggering HTML5 Form Validation) : https://stackoverflow.com/a/39689115/370786
If you group each set of inputs that you want to validate together in a form (so you will have several forms), then you can trigger validation through Javascript. Then you can stop the form from posting. This can be done through Javascript - maybe it can be done in HTML but I'm not sure.
Then once everything has validated you will need to post the data to the server, using some Javascript. It might be as simple as just triggering the submit() method on a form element.

Show ValidationErrors in Validation Summary only in asp.net

<asp:ValidationSummary ID="vsArea" runat="server" />
<div class="controls">
<asp:DropDownList ID="ddlDegreeLevel" runat="server"
placeholder="DegreeLevel" CssClass="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"
ControlToValidate="ddlDegreeLevel"
Display="None" ForeColor="Red" InitialValue="-1"
ErrorMessage="Please select degree level" Text="">
</asp:RequiredFieldValidator>
</div>
Errormessage of "rfvDegreeLevel" is show on validation-Summary and also below the control. I also set Display="None" but still show on both area .
I want to show message only on Validation summary.
This is by design...message will be shown in validation control and in the summary. Basically you have 2 options
Show separate messages in your control and validation summary. E.g. you can put a red * in the validation control and detailed error message in the summary, which is a best practice. To achieve this, set both Text and ErrorMessage property of the validation control. Text will be appearing in the control and ErrorMessage will be appearing in the summary.
hide the message from control, set the Display property of the control to none..
You have to add validationgroup to all the controls. For this you can use the following code:
<asp:ValidationSummary ID="vsArea" runat="server" ValidationGroup="degree" />
<div class="controls">
<asp:DropDownList ID="ddlDegreeLevel" runat="server"
placeholder="DegreeLevel" CssClass="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvDegreeLevel" runat="server"
ValidationGroup="degree"
ControlToValidate="ddlDegreeLevel"
Display="None" ForeColor="Red" InitialValue="-1"
ErrorMessage="Please select degree level" Text="">
</asp:RequiredFieldValidator>
</div>

How do I create a 'asp:RegularExpressionValidator' to prevent a user from using hashtags and ampersands?

Here is what I have tried
<asp:RegularExpressionValidator ID="regJobTitle" runat="server"
ControlToValidate="Job_TitleTextBox"
CssClass="required" Display="Dynamic" EnableClientScript="true"
ErrorMessage="Please remove (&) or (#)"
SetFocusOnError="true" ValidationExpression="^[^&#]*$" />
When I run the script the error message will not display.

Required Field Validator always shows the error message

I have this code:
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rvFirstName" runat="server"
ErrorMessage="Enter First Name"
ForeColor="Red" ControlToValidate="txtFirstName"
SetFocusOnError="True"></asp:RequiredFieldValidator>
The problem is that the error message shows every time the page loads not only after the submit button has been clicked. I want it to show it only if the user tries click the "Next"(submit) button (how it should work).
If this is relevant: The code above is placed in a UserControl which is included in another UserControl(the "Next" button is here) which is then included in a View of a MultiView.
Any ideas
use causeValidation to false on other buttons.
and display to dynamic.
Display="Dynamic" on require filed validator
<asp:RequiredFieldValidator ID="rvFirstName" runat="server" Display="Dynamic"
ErrorMessage="Enter First Name" ForeColor="Red"
ControlToValidate="txtFirstName" SetFocusOnError="True">
</asp:RequiredFieldValidator>
The best way is set ValidationGroup on Button and RequiredFieldValidator.
please take a look on references below, is it you have something like Page.Isvalid in your page?
References : Required Field Validator, displaying on initial page load
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" DisplayMode="BulletList"
HeaderText="Validation issues" ShowSummary="False" ValidationGroup="Validation"/>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rvFirstName" runat="server" ErrorMessage="Enter First Name" ForeColor="Red" ControlToValidate="txtFirstName" SetFocusOnError="True" ValidationGroup="Validation"></asp:RequiredFieldValidator>
Add the above code and modify your Required Field Validator
Set 'ValidationGroup' for RequiredFieldValidator and the Next button
Remove the SetFocusOnError="True" and add display="Dynamic" instead of SetFocusOnError="True" in your requiredfieldvalidation and set casevalidtion="false" in your page.
01.If you want to display message in page load. protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Page.Validate();
}
}
Display="Dynamic"

Categories

Resources