Monday, August 17, 2015

Required Field Validator Example


Design View
----------------------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>


<body>

<h3><font face="Verdana">Simple RequiredField Validator Sample</font></h3>

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

    <table bgcolor="#eeeeee" cellpadding=10>
    <tr valign="top">
      <td colspan=3>
        <asp:Label ID="lblOutput" Text="Fill in the required fields below" ForeColor="red" Font-Names="Verdana" Font-Size="10" runat=server /><br>
      </td>
    </tr>

    <tr>
      <td colspan=3>
      <font face=Verdana size=2><b>Credit Card Information</b></font>
      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Card Type:</font>
      </td>
      <td>
        <ASP:RadioButtonList id=RadioButtonList1 RepeatLayout="Flow" runat=server>
            <asp:ListItem>MasterCard</asp:ListItem>
            <asp:ListItem>Visa</asp:ListItem>
        </ASP:RadioButtonList>
      </td>
      <td align=middle rowspan=1>
        <asp:RequiredFieldValidator id="RequiredFieldValidator1"
            ControlToValidate="RadioButtonList1"
             Display="Static"
             ErrorMessage="choose the card type"
            InitialValue="" Width="100%" runat=server ForeColor="Red">
            *
        </asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Card Number:</font>
      </td>
      <td>
        <ASP:TextBox id=TextBox1 runat=server />
      </td>
      <td>
        <asp:RequiredFieldValidator id="RequiredFieldValidator2"
            ControlToValidate="TextBox1"
            Display="Dynamic"
              ErrorMessage="enter value to the cardnumber"
            Width="100%" runat=server ForeColor="Red">
           *
        </asp:RequiredFieldValidator>

      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Expiration Date:</font>
      </td>
      <td>
        <ASP:DropDownList id=DropDownList1 runat=server>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem >06/00</asp:ListItem>
            <asp:ListItem >07/00</asp:ListItem>
            <asp:ListItem >08/00</asp:ListItem>
            <asp:ListItem >09/00</asp:ListItem>
            <asp:ListItem >10/00</asp:ListItem>
            <asp:ListItem >11/00</asp:ListItem>
            <asp:ListItem >01/01</asp:ListItem>
            <asp:ListItem >02/01</asp:ListItem>
            <asp:ListItem >03/01</asp:ListItem>
            <asp:ListItem >04/01</asp:ListItem>
            <asp:ListItem >05/01</asp:ListItem>
            <asp:ListItem >06/01</asp:ListItem>
            <asp:ListItem >07/01</asp:ListItem>
            <asp:ListItem >08/01</asp:ListItem>
            <asp:ListItem >09/01</asp:ListItem>
            <asp:ListItem >10/01</asp:ListItem>
            <asp:ListItem >11/01</asp:ListItem>
            <asp:ListItem >12/01</asp:ListItem>
        </ASP:DropDownList>
      </td>
      <td>
        <asp:RequiredFieldValidator id="RequiredFieldValidator3"
          ControlToValidate="DropDownList1"
          Display ="Static"  ErrorMessage="Select the expiration date"
          InitialValue="" Width="100%" runat=server ForeColor="Red">
          *
        </asp:RequiredFieldValidator>
      </td>
      <td>
    </tr>
 
    <tr>
      <td></td>
      <td>
        <ASP:Button id=Button1 text="Validate"  runat=server />
     

     
      </td>
      <td></td>
    </tr>
    <tr>
    <td>
 
    <asp:DropDownList id="ListFormat" AutoPostBack=true OnSelectedIndexChanged="ListFormat_SelectedIndexChanged" runat=server >
    <asp:ListItem>List</asp:ListItem>
    <asp:ListItem selected>Bulleted List</asp:ListItem>
    <asp:ListItem>Single Paragraph</asp:ListItem>
</asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:ValidationSummary ID="valSum" runat="server" DisplayMode="BulletList"
                HeaderText="You must enter a value in the following fields:"
                Font-Names="verdana"
                Font-Size="12"
                />
    </td>
    </tr>
    </table>

    <br />
    Dynamic : not take any space at run time when validataion succeeds<br />
    Static : Take space at runtime even the validation succeeds<br />
    InitialValue : doesn&#39;t consider as value specified at the control need to give
    another value apart from initial value</form>

</body>
</html>
-------------------------------------------------------------------------------------------

Code behind :
-----------------

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Validate()
        End If
    End Sub

   

    Sub ListFormat_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        ' Change display mode of the validator summary when a new option
        ' is selected from the "ListFormat" dropdownlist

        valSum.DisplayMode = ListFormat.SelectedIndex
    End Sub



No comments: