2

I have an ASP.NET 4.5 Web Site project which is using Razor views. This is not an MVC project, it is an empty web site with just a few .cshtml files in it.

I have a form where I want to validate. I found a site here that explains how to do this with a Razor view, but the Validation class does not have the methods shown in the example. I then came across the required attribute in HTML5, and I added that to each <input> in my form. This is validating the form when I submit it, but it is using generic error messages like shown in the image:

enter image description here

Here are my questions:

Is there some way to customize these messages?

Is there some way to validate other requirements besides required - like making sure its a number, or a certain length, etc?

*Note** I am not using MVC, so no models, etc. I also was trying to use something built in to ASP.NET if possible, and not use jQuery Validation directly.

Thanks in advance!

therealjohn
  • 2,378
  • 3
  • 23
  • 39
  • Take a look at this pure JavaScript implementation in the accepted answer to [Set custom HTML5 required field validation message](http://stackoverflow.com/questions/13798313/set-custom-html5-required-field-validation-message). – Karl Anderson Nov 20 '13 at 16:42

2 Answers2

4

You can set your custom message with data-val-required attribute.

Make sure you added the jquery validation and Microsoft jquery unobstrustive js file in your page.

Below are the example. You can mix all data-val attributes and achieve all your combination

Example 1: Required, length

<input type="text" value="" name="UserName" id="checking-user" 
 data-val="true"
 data-val-required="The UserName field is required." 
 data-val-length-max="100" 
 data-val-length="The field UserName must be a string a maximum length of 100." 
 />

Example 2: Required, Number and Regex

<input type="text" value="" name="PaidAmount" 
   data-val="true" 
   data-val-required="Amount is required" 
   data-val-regex-pattern="[-+]?[0-9]*\.?[0-9]?[0-9]" 
   data-val-regex="Amount should be in numbers" 
   data-val-number="The field PaidAmount must be a number." 
   />
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • He said without using jquery :-o –  Nov 20 '13 at 16:44
  • @Geeo, then he should build his own library for validating :) – Murali Murugesan Nov 20 '13 at 16:45
  • Yeah but html5 has some support for validation :-) –  Nov 20 '13 at 16:47
  • Thanks Murali for the information on jQuery. I did not know it could be used like that using attributes. Is this a better solution for browser support when HTML5 may not be available? – therealjohn Nov 20 '13 at 17:36
  • @Murali, after some experimentation with your suggestion, I can't get it to work. Is using MVC required for this? Everything on the web seems to be related to using MVC with this approach. – therealjohn Nov 20 '13 at 18:30
  • @therealjohn, even the data-validation-required is not HTML 5 attribute. jQuery validation and Microsoft unobtrusive js framework reads and internally build the validation. I used in with ASP.Net webforms also it worked well. All you need is include, jquery.js, jquery.validation.js and Microsoft unobstrusive can be downloaded [here](http://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Validation/). I setup a fiddle and show you soon – Murali Murugesan Nov 20 '13 at 19:22
  • @Murali That would be great! Thanks! – therealjohn Nov 20 '13 at 20:16
1

Yeah you can do a lot of validation with HTML5 only

Along with the text input type, there are now a host of other options, including email, url, number, tel, date and many others.

You can use setCustomValidity to customize messages and whatnot.

  • Great! Is there a way to customize the messages being returned when validation fails? – therealjohn Nov 20 '13 at 17:35
  • @therealjohn have you bothered reading or googling "html5 validation custom message"? -> http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-setcustomvalidity –  Nov 20 '13 at 17:43
  • No I didnt. Thanks for the help. – therealjohn Nov 20 '13 at 17:51