Manually Add Error Validationsummary
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies validationsummary add message programmatically of this site About Us Learn more about Stack Overflow the company add error message to validation summary using jquery Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Add Error Message To Validation Summary Using Javascript
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute:
Add Error To Validationsummary Mvc
Sign up On postback, how can I add a error message to validation summary? up vote 50 down vote favorite 12 Two questions: On postback when a user clicks submit, how can I add a error message to validation summary? Is it also possible to highlight a particular textbox using the built in .net validation controls? asp.net validation share|improve this question edited add custom validator to validation summary Apr 22 '09 at 15:57 TStamper 22.1k85069 asked Apr 22 '09 at 15:39 Blankman 64.5k197564924 add a comment| 5 Answers 5 active oldest votes up vote 71 down vote Dynamically create a CustomValidator control and add it directly to the Page.Validators collection. Dim err As New CustomValidator err.ValidationGroup = "MyGroup" err.IsValid = False err.ErrorMessage = "The password is invalid" Page.Validators.Add(err) Unlike adding the CustomValidator to the markup, this method allows you to add any number of arbitrary error messages based on server-side business logic. Note that you can also add it to the page directly, but there are a couple of rules to follow: You must add the control to the same naming container as the controls of the validation group. If you don't want the validation message to appear in a random position in the page, you will either have to add the validator to a specific container or you will need to supress it using a CSS class or style. You can also create a custom class and implement IValidator, which enables you to add the message with one line of code,
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the
Add Message To Validation Summary Javascript
company Business Learn more about hiring developers or posting ads with us Stack Overflow add error message to validationsummary Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 validationsummary message million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Add error message to @Html.ValidationSummary up vote 21 down vote favorite 7 I'm using standard MVC3 Razor views with http://stackoverflow.com/questions/777889/on-postback-how-can-i-add-a-error-message-to-validation-summary unobtrusive Javascript validation, using @Html.ValidationSummary to show them at the top of the form. If the standard validations (things like [Required]) pass, I then run some very custom client-side validation that fires when the user hits the Submit button. (The validation looks across a number of form elements to make sure that the proper set of them have been checked, etc., so it's not as simple as just creating a new custom validator http://stackoverflow.com/questions/7882008/add-error-message-to-html-validationsummary for a single field). I'd like the possible error(s) I construct there to be shown in the ValidationSummary list, but I can't figure out how to get the error message to appear there. asp.net-mvc-3 unobtrusive-validation share|improve this question edited Aug 22 '12 at 9:08 Eranga 26.4k36478 asked Oct 24 '11 at 21:10 Kras 2532612 In client-side or server-side? –Javad_Amiry Oct 24 '11 at 21:16 add a comment| 2 Answers 2 active oldest votes up vote 33 down vote In client-side: function YourCustomValidator() { // do your validation logic here via JavaScript return true; // or false based on your validation logic } $(document).ready(function () { // take your own form-selector like ("form", this) $("form", this).first().submit(function () { return (YourCustomValidator() && $(this).valid()); }); }); OR In server-side: Think you have a model like this: public class Test { [Required] [StringLength(100)] public string FullName { get; set; } } and when you are validating it: if(ModelState.IsValid) { // default validations run here if(/* some custom validations run here, there is an error about "FullName" */){ // you should set the "key" for Model-Error to "FullName" ModelState.AddModelError("FullName","error-message goes here") } if(/* some custom validations run here, the error is global, not on "FullName" */){ // you should set the "key" for Model-Error to an empty-string ModelState.AddModelError("","error-message goes her
J InceFebruary 28, 200819 Share 0 0 For a while now I’ve used this handy bit of code to add a message programmatically to a Validation Summary control, without associating it with a Validator. I’ve no idea where it came from – perhaps my head, https://blogs.msdn.microsoft.com/simonince/2008/02/28/adding-messages-to-a-validation-summary/ perhaps someone cleverer than I… so if it was from you, shout up! I was asked how to do this today by a customer, so I felt inspired to blog it. Anyway, sometimes you get an error from your business logic that it just isn’t practical to have pre-validated. For example, when adding a new employee to a database, perhaps the employee name has a UNIQUE constraint on it. Validating this up front to validation might not be easy… So if I get an error back from my business logic (either in the form of a list of validation errors, or in the worst case scenario as an exception) how do I display this message to the user? Well it turns out this is quite easy – just add a validator that is reporting itself as “IsValid = false” to the Page.Validators collection. Consider the following class; public class message to validation ValidationError : IValidator { private ValidationError(string message) { ErrorMessage = message; IsValid = false; } public string ErrorMessage { get; set; } public bool IsValid { get; set; } public void Validate() { // no action required } public static void Display(string message) { Page currentPage = HttpContext.Current.Handler as Page; currentPage.Validators.Add(new ValidationError(message)); } } (Note: This is usingautomatic properties - a C# 3.0 feature. Alter the code to use standard properties if you're using an earlier version of .NET) This immediately allows me to use the following code; ValidationError.Display("Oops, some error occurred."); Succinct, eh?! Here’s a shot of it in action;
Tags ASP.NET C# Comments (19) Cancel reply Name * Email * Website Jason says: February 29, 2008 at 12:07 pm Thanks for this. It definitely came in useful for me. Reply KA says: March 28, 2008 at 1:15 pm This works well - thanks for the tip. Reply Nicole says: April 7, 2008 at 2:06 pm Something similar to this can be done by adding a CustomValidator dynamically when the error occurs. For example: CustomValidator cv = new CustomValidator(); cv.IsValid = false; cv.ErrorMessage = "The error to display."; this.Page.Vali