Validation Controls in Asp.Net

Validations with asp.net page

1)ensuring proper entry of data from user is called “validation”

Ex: name cannot be blank

       Mobile number should be 10 digits

2)asp.net provides validation controls with built in logic to validate user input

3)validation controls will support client side validation (or) server side validation. This makes developer job easier and faster.

 

Asp.net is providing following validation controls

1)range validation

2)required field validator

3)compare validator

4)regular expression validator

5)custom validator

6)validation summary

 

Common properties of validation controls

1)control validate

           Specify stand webserver controlid to validate user input

2)text

          Specify error information to be displayed if user is providing an invalid input

3)error message

          Specify error information to be displayed with validation summary control user is providing an invalid input

4)set focus on error-true/false

          True-the cursor will be placed in the same textbox if user input is invalid, If user input is valid then cursor will be placed in the next textbox

         False[default]-cursor will be placed in the next textbox irrespective of user input valid (or) invalid

5)enable client script-true/false

        True[default]-client side validation

        False-server side validation

 

Range validator

->this control can be used to validate user input within required range(specified range)

->properties

   1)minimum value-specify lower bound

   2)maximum value-specify upper bound

 

Example on range validator

Goto visual studio

Start->run->devenv

It will display main window of visual studio

File menu->new->website->visual c#->select asp.net empty website

Weblocation->e:\aspnet\validationsite[drive:\dir\websitename]

Visual studio create a folder with website name, in this folder website related files will be placed

 

Add webform

Goto view menu and select solution explorer

Right click on website path and select add new item

Select webform

Give name as default.aspx

       

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

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

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    </form>

</body>

</html>

 

Goto design part design the form like below

 

     Age[label1]                                       

 


    Mobilenumber[label2]                                rangevalidator1

 

submit

                                                                    

 

      

 

->rangevalidator1 available in toolbox->validation

 

Rangevalidator1 properties

Controlto validate-textbox1

Minimum value-25

Maximum value-40

Text-invalid age

Backcolor-select red

Forecolor-select white

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write("<h2>submitted </h2>");

    }

}

 

Goto contrl f5

 

Note:

1)valiation control will download javascript to browser, this javascript will be executed to validate user input into textbox when cursor is leaving textbox

2)button click will not perform webpage submission if page validations are not successful

3)till asp.net 2.0 javascript logic used by validation controls will be visible to developer in the form of jsfile, later versions doesn’t provide jsfile to developer

4)a single validation control can validate only one standard webserver control[textbox]

5)a single standard webserver control[textbox] can be binded with more than one validations control

6)range validator will validate textbox if input is given, If textbox is blank then it will not perform validation

7)requiredfieldvalidator can be used to validate whether textbox is given with input (or) not

8)in the above example age textbox requires 2 validation control [requiredfieldvalidator and rangevalidator]

 

Example on requiredfieldvalidator1

Goto visual studio

Start->run->devenv

It will display main window of visual studio

File menu->new->website->visual c#->select asp.net empty website

Weblocation->e:\aspnet\validationsite[drive:\dir\websitename]

Visual studio create a folder with website name, in this folder website related files will be placed

 

Add webform

Goto view menu and select solution explorer

Right click on website path and select add new item

Select webform

Give name as default.aspx

 

Goto design part design the form like below

 

     Age[label1]                                       

 


    Mobilenumber[label2]                                rangevalidator1  requiredfieldvalidator

 

submit

                                                                     

Requiredfieldvalidator properties

Controltovalidate-textbox1

Text-enterage

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        TextBox1.Attributes.Add("onblur", "validatorvalidate(" + RequiredFieldValidator1.ClientID + ")");

    }

}

 

Goto contrl f5

 

Note:

1)by default requiredfieldvalidator will validate textbox for button click this can be changed to textbox loosing focus by executing following statement

Ex:textbox1.attributes.add(..)

2)by default validation control will occupy space with in webpage irrespective of validation is failed or not this can be changed using display property.