JumpStart # 15–Unobstructive Validation in ASP.Net 4.5

Before looking at the code, Read about – What is JumpStart?

ASP.Net 4.5 supports UnObstructive JavaScript validation on client side with UnobtrusiveValidationMode="WebForms" attribute on page level. With this advancement, page size will reduce drastically for a page which contains large number of validation controls.

Unobstructive Validation removes inline JavaScript which is generated by regular ASP.Net Validators, instead it uses HTML5 Data attributes to perform validation.

Also the same can be achieved for application level by using either of below mentioned ways-

  1. <add name="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" /> in appsettings on web.config.
  2. Globally setting the static System.Web.UI.ValidationSettings.UnobtrusiveValidationMode property to UnobtrusiveValidationMode.WebForms (typically in the Application_Start method in the Global.asax file).

*** Following results are generated using Chrome Browser Developer Tools (F12)

Webform rendered with Normal ValidationMode –

UbobstructiveValidation1

Webform rendered with UnObstructive ValidationMode –

UbobstructiveValidation2

 

Sample Code used to generate this page –

<%@ Page Language="C#" UnobtrusiveValidationMode="WebForms" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Country> _repeaterSource = new List<Country>();
        _repeaterSource.Add(new Country() { Name = "USA" });
        _repeaterSource.Add(new Country() { Name = "India" });
        _repeaterSource.Add(new Country() { Name = "Canada" });
        _repeaterSource.Add(new Country() { Name = "Australia" });
        _repeaterSource.Add(new Country() { Name = "Japan" });
        _repeaterSource.Add(new Country() { Name = "France" });
        _repeaterSource.Add(new Country() { Name = "Venice" });
        _repeaterSource.Add(new Country() { Name = "Germany" });
        _repeaterSource.Add(new Country() { Name = "Spain" });
        _repeaterSource.Add(new Country() { Name = "Portugal" });

        Repeater1.DataSource = _repeaterSource;
        Repeater1.DataBind();
    }

    public class Country
    {
        public string Name { get; set; }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" ItemType="Country">
            <ItemTemplate>
                <div>
                    Enter Country :                
                <asp:TextBox
                    ID="TextBox1"
                    Text="<%# Item.Name %>"
                    runat="server">
                </asp:TextBox>
                    <asp:RequiredFieldValidator
                        ID="RequiredFieldValidator1"
                        runat="server"
                        ControlToValidate="TextBox1"
                        ErrorMessage="RequiredFieldValidator">
                    </asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator
                        ID="RegularExpressionValidator1"
                        runat="server"
                        ControlToValidate="TextBox1"
                        ValidationExpression="(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$"
                        ErrorMessage="RegularExpressionValidator">
                    </asp:RegularExpressionValidator>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

You may also like...

One Pingback/Trackback

    18 November 2012 at 5:11pm
    ... [Trackback]... [...] Read More here: ...
  • My Homepage
  • Pingback: My Homepage()

  • youker

    I thoroughly enjoyed this post