HTML and CSS Reference
In-Depth Information
Figure 5-15. Validations in an MVC controller
When you have validation logic at the client side as well as the server side, the client-side validations
are performed prior to submitting the form. In this example, the HTML5 validations are performed first,
and only then is the form submitted. Once the control reaches the Index() action method. the server-side
validations are performed.
HTML5 Input Types and Unobstructive Validation
Unobstructive validation is a technique implemented by ASP.NET that uses data-annotation attributes and
jQuery. Under this scheme, a data model is decorated with data-annotation attributes that perform
validations on the data model property values. At runtime, ASP.NET emits data-* attributes, a feature of
HTML5. ASP.NET performs client-side validations with the help of these emitted data-* attributes and
jQuery.
The data-* attributes are custom attributes and are just like any other built-in HTML attributes in
terms of usage syntax. However, they differ from built-in HTML attributes in that they don't play any role
in the visual rendering of the element. ASP.NET 4.5 Web Forms as well as MVC applications support
unobstructive validation. I don't discuss the unobstructive validation scheme any further; suffice to say
that you can use unobstructive validation along with HTML5 validation techniques if required.
n Note The data-* attributes are a feature of HTML5. You learn to use them in Chapter 6.
Customizing Validation Messages
So far, you've relied on the browser to display HTML5 validation messages. No doubt browsers supporting
HTML5 validations do a good job of displaying validation messages neatly. However, you may want to
customize them further. For example, you may want the error message font and color to match your web
page theme, or you may want to give a strong highlight to any input control that violates the validation
rules. HTML5 offers two ways to accomplish such customizations:
• Use CSS pseudo-classes to customize the appearance of an input ield that violates a
validation rule.
 
Search WWH ::




Custom Search