how to display only single message for multiple RequiredFieldValidators? - asp.net

how to display only single message for multiple RequiredFieldValidator instead of individual message for RequiredFieldValidator ?
i want to as shown in following image..
my view is..

You will have to use ValidationSummary control for this. See this ValidationSummary Class MSDN article for details and example on how to do this. This article contains an example of what you are trying to figure out exactly.

set the HeaderText to something like "(*) Fields are required" to your validation summary.

you can leave error message field of each RequiredFieldValidator blank and put * in text field, then add ValidationSummary defining its header text with error msg this will works for your scenario.
<asp:RequiredFieldValidator ID="RequiredFieldValidator_overhead_name" runat="server" ControlToValidate="TextBox_overhead_name">*</asp:RequiredFieldValidator>
<asp:ValidationSummary ID="ValidationSummary_overhead_estimate" runat="server" DisplayMode="SingleParagraph" HeaderText="please insert data into fileds" />

This gentleman solved it here quite simply: http://www.cactusoft.com/blog_40

I can see what you are trying to do but it's difficult with the ASP.Net validators
The only way I can think to do it is remove the ValidationSummary altogether and manually create your own using the ASP.Net validator API and JQuery i.e.
Change all required validators to ErrorMessage = "*" Remove Text value
Remove Validation Summary
Add a label at the bottom to function as a custom validator summary. Style it red
In the page markup script something like
if(!Page_IsValid) {
$('#myCustomValidatorSummary').text('Please fill in required fields')
}
Page_IsValid is from the ASP.Net validator API. Set to false if the page fails validation.
Of course this assumes that you only have the required field validators on your form. If there is a mix then you will need to check if one or more of the required ones have failed by iterating through the Page_Validators on the client using JQuery/javascript
Honestly though I wouldn't do it - it's too hard
I would just do this - For each required field validator - set
Text="*"
ErrorMessage="[Field Name] is mandatory. Please supply a value." or similar.

Quick and easy way: add a CssClass to the ValidationSummary, then a css style that sets ul elements under that class to display: none.
For example:
<style>
.validationSummary ul {display:none}
<stlye>
...
<asp:ValidationSummary CssClass="validationSummary" ...

You should use the ValidationSummary Control from ASP.NET in addition to ValidationSummary you could also use the Group property to separate controls into logical groups. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.aspx for a bunch of examples.

use ValidationSummary
The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page.
The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.
http://asp-net-example.blogspot.in/2008/10/validationsummary-example-how-to-use.html

To add to Mike Godin's answer, for only showing a single alert message for multiple field validators:
Keep individual required messages. Add Validation Summary with DisplayMode="BulletList" and HeaderText="Please provide the required information above."
The "BulletList" display mode produces a unordered list of LI's inside the Validation Summary DIV, then hide UL via styling - only the "HeaderText" will show:
#validationSummary ul {
display:none;
}
<asp:ValidationSummary
id="validationSummary"
DisplayMode="BulletList"
EnableClientScript="true"
HeaderText="Please provide the required information above."
ValidationGroup="btnSubmit"
runat="server"/>

Related

HTML5 Types in ASP.NET

Site will exclusively be used on mobile devices. So for fields requiring only numeric input, I want to bring up the numeric keypad. I can successfully do this by using an html input element with the type set to tel. I want to add an asp:RequiredFieldValidator to this field and based on MSDN.
I need to set the input to runat="server". When I do this, I get this error;
tel is not a valid type for an input tag.
If I remove the runat="server", I get
Unable to find control id Contract referenced by the ControlToValidate property of ''
Here is my code:
<input type="tel" runat="server" id="Contract"></input>
<asp:RequiredFieldValidator runat="server" ControlToValidate="Contract"
ValidationGroup="IfOnRent" Text="*"
ErrorMessage="Contract Required">
</asp:RequiredFieldValidator>
Am I just out of luck and have to code my own validations?
You may want to leave "type" undeclared in your code "front". You can set this in the code behind (maybe in the Page_Init or Page_Load):
this.Contract.Attributes.Add("type", "tel");
If you have Microsoft .NET Framework 4 Reliability Update 1 (KB2533523) installed, than TextBox accepts new html5 input types (you would need it for UpdatePanel to recognize them on postback too).
Or you can just inherit HtmlInputText in your own control (any .net version - any input type):
[ToolboxData("<{0}:Input runat='server' />"), Themeable(true)]
public sealed class Input: HtmlInputText { }
If you are using C#
Contract.Attributes["type"]="tel"
Writing inline types for html inputs may produce parse error in .net. So add the above line of code in your PageLoad()
you can use
Make sure that the submit button has the same validationgroup property value as the requiredfieldvalidator

Server side validation

I am having an issue with the requiredfieldvalidator control not working on an ASP.net page. I have completed the attributes of that field properly, but when I test it, the postback is allowed to happen even if the field in question is blank.
So I want to do server side validation instead. What is the best way to do that? In the event that caused the postback? Also, if I find out the field is blank, how do I get the user back to the screen with all other values they placed on other fields intact and a message saying "This field cannot be blank".
EDIT:
This is the code:
<asp:TextBox ID="fName" TabIndex="1" runat="server" Width="221px" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="FNameRequiredFieldValidator" runat="server" ControlToValidate="fName" InitialValue="" ErrorMessage="Filter Name cannot be blank." ToolTip="Filter Name cannot be blank.">*</asp:RequiredFieldValidator>
You need to provide the markup for your Button / Link control as well.
The 'CausesValidation' attribute is not supposed to be used on TextBox controls.
The button you click needs to have that attribute set to "True".
Please provide that markup and then I can advise on the alternate server side validation.
To enable Client-side Validation, set the EnableClientScript="true" on the RequiredFieldValidator.
You should also always validate on the server side too. But the RequiredFieldValidator doesn't let you do any special-handling server-side. Just check if Page.IsValid(). This will return false if the field is not supplied.
If you want to do custom validation, use a CustomValidator.

Use HtmlEncode in Details View TemplateItem Control

I have details view control in my asp.net web form, which on of its item template gets it is value from database, and show this into a richtextbox :
<FTB:FreeTextBox id="txtDescription" runat="Server" AllowHtmlMode="false" Text='<%# (Eval("Description") )%>'
>
</FTB:FreeTextBox>
but when i click on insert or update button, i get the following error :
A potentially dangerous Request.Form value was detected from the client ....
i tried this :
Text='<%# HttpUtility.HtmlDecode((string)Eval("Description"))%>'
bu it did not work ethier, and i got the error again.
is there any way except turning validateRequest off.
Would you please help me?
No, there isn't a way to get this to work aside from turning Validate Request off. Which isn't a bad thing if you write your database functionality correctly and implement strict custom form validation.

How do you reenable a validation control w/o it simultaneously performing an immediate validation?

When I called this function to enable a validator from client javascript:
`ValidatorEnable(document.getElementById('<%=valPassportOtherText.ClientID%>'), true); //enable` validation control
the required validation control immediately performed it validation, found the value in the associated text box blank and set focus to the textbox (because SetFocusOnError was set to true). As a result, the side effect was that focus was shifted to the control that was associated with the Validation control, i teh example, txtSpecifyOccupation.
<asp:TextBox ID="txtSpecifyOccupation" runat="server" AutoCompleteType="Disabled"
CssClass="DefaultTextBox DefaultWidth" MaxLength="24" Rows="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="valSpecifyOccupation" runat="server" ControlToValidate="txtSpecifyOccupation"
ErrorMessage="1.14b Please specify your <b>Occupation</b>"
SetFocusOnError="True"> Required</asp:RequiredFieldValidator>
Perhaps there is a way to enable the (required) validator without having it simultaneously perform the validation (at least until the user has tabbed off of it?)
I'd like validation of the txtSpecifyOccupation textbox to occur only on a Page submit or when the user has tabbed of the required txtSpecifyoccupation textbox.
How can I achieve this?
Don't call the ValidatorEnable method. Instead, get a reference to teh validation control and simply set its "enabled" property as desired.
To clear the validation control's error text, set the innerText property to "".

asp.net sanitizing user input

Does asp.net have a built in mechanism that can sanitize all textbox input instead of redirecting to the error page?
I have a textbox input where a user can enter a name, but if they try to enter and <> tags the page automatically throws an error. I just want to handle this error in a user friendly way.
You'll want to look at the AntiXSS library for that. It's a dll so it's easy to drop in and start using it.
The download is at CodePlex.
You can use the ASP.NET RegularExpressionValidator control with a pattern like: ^[^<>]*$
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="The <> tags are not allowed!"
ValidationExpression="[^<>]*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.
The benefit of doing it this way is that the error can be handled gracefully and the user can be notified on the same page.
However, if you need to do this for many textboxes and you just want to present something nicer than the error page, you could handle the ValidateRequest error to provide a friendlier message and keep the user on the same page (not just replace it with a custom error page). For more info, check out Kirk Evans' post: Handling ValidateRequest errors within a Page (refer to the section titled Overriding the OnError Method).
Read this for a step-by-step: http://yourtahir.wordpress.com/2008/03/28/aspnet-not-allow-html-in-text-boxserver-error-in-application-a-potentialy-dangerous-requestform-value-was-detected/
You have to do some web.config work.
ASP.net has validation controls
[http://msdn.microsoft.com/en-us/library/7kh55542.aspx][1]
Also there is Mark Down Editor which is a control that strips out html tags etc.

Resources