ASP.NET: ValidationSummary doesn't display any contents - asp.net

I'm trying to figure out how the heck the validation summary control of ASP.NET (3.5 I think) works.
<asp:ValidationSummary ID="vldSummary" runat="server" DisplayMode="BulletList"
CssClass="error" EnableClientScript="true" />
<asp:RequiredFieldValidator ID="vldSubject" ControlToValidate="txtSubject"
EnableClientScript="false" Text="You must enter a subject." runat="server" />
<asp:RequiredFieldValidator ID="vldMessage" ControlToValidate="txtMessage"
EnableClientScript="false" runat="server" Text="You must enter a message." />
It seems that no matter what I do, the validation summary remains empty (but is rendered) and the errors are only displayed at the position of each respective validator.
What am I doing wrong?

Text property's value is what is displayed beside the control. You need to set the ErrorMessage property of the validators to control what is shown in the summary.

You want to set the ErrorMessage property on your validation controls. This text will be displayed by the ValidationSummary control.
Try:
<asp:ValidationSummary ID="vldSummary" runat="server" DisplayMode="BulletList" CssClass="error" EnableClientScript="true" />
<asp:RequiredFieldValidator ID="vldSubject" ControlToValidate="txtSubject" EnableClientScript="false" ErrorMessage="You must enter a subject." runat="server" />
<asp:RequiredFieldValidator ID="vldMessage" ControlToValidate="txtMessage" EnableClientScript="false" runat="server" ErrorMessage="You must enter a message." />

Set the ErrorMessage property on the RequiredFieldValidators, not the Text property.
<asp:RequiredFieldValidator ID="vldSubject" ControlToValidate="txtSubject" EnableClientScript="false" ErrorMessage="You must enter a subject." runat="server" />
<asp:RequiredFieldValidator ID="vldMessage" ControlToValidate="txtMessage" EnableClientScript="false" runat="server" ErrorMessage="You must enter a message." />

Related

ASP.NET Required Field Validator & Custom Validator not working

This is my aspx code
<p class="input-block last">
<asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10" ValidationGroup="EmployerRegister" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="uxEmployerName" ErrorMessage="You must enter Company Name." EnableClientScript="false" Display="none" ValidationGroup="EmployerRegister" />
</p>
<p class="input-block last">
<asp:TextBox ID="uxEmail" runat="server" CssClass="uxemail" TabIndex="16" ValidationGroup="EmployerRegister" />
<asp:RequiredFieldValidator ID="uxEmailValReq" runat="server" ControlToValidate="uxEmail" ErrorMessage="You must enter Email." EnableClientScript="false" Display="none" ValidationGroup="EmployerRegister" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="The 'Email Address' you have entered does not appear to be a valid address." ControlToValidate="uxEmail" EnableViewState="False" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="EmployerRegister" Display="None" />
</p>
<p class="input-block last">
<asp:LinkButton runat="server" ID="uxRegisterButton" Text="Submit" CausesValidation="true" ValidationGroup="EmployerRegister" CssClass="button" TabIndex="18" />
<a class="button" href="#" tabindex="17">Edit package</a>
</p>
the only validator works here is RegularExpressionValidator
if I input wrong email format then it shows the error message. but not if i leave the email field.
What am i doing wrong. any solution
i have also added a custom validator with the email field
<asp:CustomValidator ID="uxEmailValUnique" runat="server" ErrorMessage="It appears that you already have an account with us. <br/><a href='forgotten-password.aspx'>Request password</a>"
ControlToValidate="uxEmail" EnableViewState="False" ValidationGroup="EmployerRegister"
Display="None" />
the backend server code for this is
Private Sub uxEmailValUnique_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles uxEmailValUnique.ServerValidate
args.IsValid = Not LocalHelper.EmailExists(uxEmail.Text)
End Sub
where in LocalHelper file
Shared Function EmailExists(email As String) As Boolean
Return DB.GetInteger("select count(id) from [user] where deleted = 0 and active = 1 and email = #email", DB.SIP("email", email)) > 0
End Function
this does not work either
what am i doing wrong.
I assume that you have a ValidationSummary control similar to this:
<asp:ValidationSummary runat="server" ForeColor="Red" ValidationGroup="EmployerRegister" />
If any of your validators has client-side validation enabled (EnableClientScript="true", the default value), it will prevent the postback when the client validation fails, so that server-side validation will not be performed. In your current code, the RegularExpressionValidator has client-side validation enabled (not explicitly disabled, in fact); if it fails, the server-side validation for the required fields is not performed and the other error messages are not displayed.
You could disable the client-side validation for all your validators. The message would appear for all of them (if appropriate) every time you submit the form.
<asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="false" ... />
<asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="false" ... />
<asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="false" ... />
<asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
Another reasonable approach could be to enable the client-side validator for the most basic requirements, and disable it for the CustomValidator. The more advanced validation does not make much sense if the e-mail is empty or not valid in the first place.
<asp:RequiredFieldValidator ID="uxEmployerNameValReq" EnableClientScript="true" ... />
<asp:RequiredFieldValidator ID="uxEmailValReq" EnableClientScript="true" ... />
<asp:RegularExpressionValidator ID="uxEmailExprVal" EnableClientScript="true" ... />
<asp:CustomValidator ID="uxEmailValUnique" EnableClientScript="false" ... />
N.B. In your question, you say that the validation message is not displayed when the e-mail field is empty. I haven't seen that behavior with your original code in my tests.

RequiredFieldValidator not firing, but RangeValidator is fine

My RangeValidator is working perfectly. My RequiredFieldValidator is not firing at all. Any ideas why?
<asp:TextBox ID="txtTestNumber" runat="server" MaxLength="4"
Text='<%# eval("LC") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTestNumber" runat="server"
Display="Dynamic" EnableClientScript="False" ErrorMessage="Required"
ForeColor="Red" ControlToValidate="txtTestNumber" Enabled="true"
ValidationGroup="WizStepValidationGroup">required
</asp:RequiredFieldValidator>
<asp:RangeValidator ValidationGroup="WizStepValidationGroup"
Display="Dynamic" ID="rvTestNumber" runat="server" ErrorMessage="Invalid value"
MinimumValue="0" MaximumValue="9999" ControlToValidate="txtTestNumber"
Type="Integer"></asp:RangeValidator>
Thanks for your input!
Add the ValidationGroup="WizStepValidationGroup" property to the text box and the button.
UPDATE:
You have set EnableClientScript="False" of RequiredFieldValidator hence it is not getting validated at the client side. Hence remove the EnableClientScript property or set it to True and I am 100% sure then it will work.

RegularExpressionValidator in Asp.net

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

Prevent textbox from firing validators

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.

Required and RegularExpression Validators taking up space

I'm having issues with asp.net's field validators taking up space on my page. I've searched around, and documentation says to use Display="Dynamic" to keep the validators from taking up space. When I use this however, the error messages are always displayed.
What am I doing wrong?
I just want the error messages to display when the user either clicks the "Save" button, or loses focus on a textbox. And I don't want the validators to take up space.
<p>Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGgroup="Email"
ErrorMessage="Please enter an email"
ControlToValidate="TextBoxEmail" runat="server"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2"
ValidationGroup="Email"
ControlToValidate="TextBoxEmail"
ErrorMessage="Please enter valid email"
runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" />
<p>Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:LinkButton ValidationGroup="Email" runat="server" Text="Save" OnClick="linkbuttonSave_Click" />
Only thing I notice in your code is you have a typo in ValidationGgroup="Email". It should be ValidationGroup="Email"
Other than that, Display="Dynamic" works for me.
After clicking, submit button -
<p>
Please enter a new email:</p>
<asp:TextBox runat="server" MaxLength="255" ID="TextBoxEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="Email"
ErrorMessage="Please enter an email" ControlToValidate="TextBoxEmail" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Email"
ControlToValidate="TextBoxEmail" ErrorMessage="Please enter valid email" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" />
<p>
Please re-enter your email:</p>
<asp:TextBox runat="server" ID="TextBoxEmail2" />
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="TextBoxEmail"
ControlToValidate="TextBoxEmail2" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Must match." ValidationGroup="Email"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="Email"
ErrorMessage="Please enter an confirm email" ControlToValidate="TextBoxEmail2" runat="server"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:LinkButton ID="LinkButton1" ValidationGroup="Email" runat="server" Text="Save"
OnClick="linkbuttonSave_Click" />
You can set the property Display to "Dynamic" or "none", the second one will cause the Error message to be displayed only in the validation summary.
I got it to work great by deleting the "ValidationGroup=.." . Havent looked what up what this does but it works great without it and not sure why it would be needed (given the validator controls have the control id of the email textbox)

Resources