Mobile Content

Friday, January 27, 2012

Validation control in asp.net

1. RequiredFieldValidator
The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid

 <form runat=server>

        Name: <asp:TextBox id=Text1 runat="server"/>

        <asp:RequiredFieldValidator id="RequiredFieldValidator1" ControlToValidate="Text1" Font-Names="Arial" Font-Size="11" runat="server">
            Required field!
        </asp:RequiredFieldValidator>

        <p>
        
        <asp:Button id="Button1" runat="server" Text="Validate" />

    </form>

It will display message when any field or control is left blank
We can validate any control using ControlToValidate property.

2.RangeValidator
The RangeValidator control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation: ControlToValidatecontains the value to validate, MinimumValue defines the minimum value of the valid range, and MaximumValue defines the maximum value of the valid range. These constants are stored as string values, but are converted to the data type defined by Type when the comparison is performed
<form runat="server">

      <table bgcolor="#eeeeee" cellpadding=10>
      <tr valign="top">
        <td>
            <h5><font face="Verdana">Value to Check:</font></h5>
            <asp:TextBox Selected id="txtComp1" runat="server"/>
        </td>
        <td>
            <h5><font face="Verdana">Data Type: Integer Min(1), Max(10)</font></h5>
        </td>
        <td>
             <asp:Label id="lblOutput1" Font-Names="verdana" Font-Size="10pt" runat="server" />
        </td>
      </tr>
      <tr valign="top">
        <td>
            <h5><font face="Verdana">Value to Check:</font></h5>
            <asp:TextBox Selected id="txtComp2" runat="server"/>
        </td>
        <td>
            <h5><font face="Verdana">Data Type: Date Min(2000/1/1), Max(2001/1/1)</font></h5>
        </td>
        <td>
             <asp:Label id="lblOutput2" Font-Names="verdana" Font-Size="10pt" runat="server" />
        </td>
      </tr>
      <tr valign="top">
        <td>
            <h5><font face="Verdana">Value to Check:</font></h5>
            <asp:TextBox Selected id="txtComp3" runat="server"/>
        </td>
        <td>
            <h5><font face="Verdana">Data Type: String Min(Aardvark), Max(Zebra)</font></h5>
        </td>
        <td>
             <asp:Label id="lblOutput3" Font-Names="verdana" Font-Size="10pt" runat="server" />
        </td>
      </tr>
     </table>

     <asp:Button Text="Validate" ID="Button1" onclick="Button1_Click" runat="server" />

     <asp:RangeValidator
        id="rangeValInteger"
        Type="Integer"
        ControlToValidate="txtComp1"
        MaximumValue="10"
        MinimumValue="1"
        runat="server" ErrorMessage="Please Enter Value between 1 to 10"/>

     <asp:RangeValidator
        id="rangeValDate"
        Type="Date"
        ControlToValidate="txtComp2"
        MaximumValue="2001/1/1"
        MinimumValue="2000/1/1"
        runat="server" ErrorMessage="Please enter valid date"/>

     <asp:RangeValidator
        id="rangeValString"
        Type="String"
        ControlToValidate="txtComp3"
        MaximumValue="Zebra"
        MinimumValue="Aardvark"
        runat="server" ErrorMessage="Please enter valid data"/>
     <br>
 

    </form>

ErrorMessage is shown when any data crosses the given range

3.RegularExpressionValidator
The RegularExpressionValidator control confirms that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on. 
RegularExpressionValidator uses two key properties to perform its validation: ControlToValidate contains the value to validate, and ValidationExpression contains the regular expression to match. 
  We can validate the content using regularexpressionvalidator
such as TextBox must contain only string or int etc..

 <table bgcolor="#eeeeee" cellpadding=10>
    
      <tr valign="top">
        <td colspan=3>
          <asp:Label ID="lblOutput" Text="Enter a 5 digit zip code" Font-Names="Verdana" Font-Size="10pt" runat="server"/>
        </td>
      </tr>

      <tr>
        <td colspan=3>
          <font face=Verdana size=2><b>Personal Information</b></font>
        </td>
      </tr>
      
      <tr>
        <td align=right>
          <font face=Verdana size=2>Zip Code:</font>
        </td>
        <td>
          <ASP:TextBox id=TextBox1 runat=server />
        </td>
        <td>
          <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
              ControlToValidate="TextBox1"
              ValidationExpression="\d{5}"
              Display="Static"
              Font-Names="verdana" 
              Font-Size="10pt">
                 Zip code must be 5 numeric digits
          </asp:RegularExpressionValidator>
        </td>
      </tr>
          
      
    </table>

For String use: [a-zA-Z\s]+
For Integer use:[0-9\s]+

3. CompareValidator
uses three key properties to perform its validation. ControlToValidate and ControlToCompare contain the values to compare. Operator defines the type of comparison to perform, for example, Equal or Not Equal. CompareValidator performs the validation by evaluating these properties as an expression, as shown in the following example

<ControlToValidate> <Operator> <ControlToCompare> 

<table bgcolor="#eeeeee" cellpadding=10>
      <tr valign="top">
        <td>
            <h5><font face="Verdana">String 1:</font></h5>
            <asp:TextBox Selected="True" id="txtComp" runat="server"></asp:TextBox>
        </td>
        <td>
            <h5><font face="Verdana">Comparison Operator:</font></h5>

            <asp:ListBox id="lstOperator" OnSelectedIndexChanged="lstOperator_SelectedIndexChanged" runat="server">
                    <asp:ListItem Selected="True" Value="Equal" >Equal</asp:ListItem>
                    <asp:ListItem Value="NotEqual" >NotEqual</asp:ListItem>
                    <asp:ListItem Value="GreaterThan" >GreaterThan</asp:ListItem>
                    <asp:ListItem Value="GreaterThanEqual" >GreaterThanEqual</asp:ListItem>
                    <asp:ListItem Value="LessThan" >LessThan</asp:ListItem>
                    <asp:ListItem Value="LessThanEqual" >LessThanEqual</asp:ListItem>
            </asp:ListBox>
        </td>
        <td>
            <h5><font face="Verdana">String 2:</font></h5>
            <asp:TextBox id="txtCompTo" runat="server"></asp:TextBox><p>
            <asp:Button runat=server Text="Validate" ID="Button1" onclick="Button1_OnSubmit" />
        </td>
      </tr>
      </table>

      <asp:CompareValidator id="comp1" ControlToValidate="txtComp" ControlToCompare = "txtCompTo" Type="String" runat="server"/>
4. CustomValidator
The CustomValidator control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script, such as JScript or VBScript. For client-side custom validation, the name of the custom function must be identified in the ClientValidationFunctionproperty. The custom function must have the form function myvalidator(source, arguments) Note that source is the client-side CustomValidator object, andarguments is an object with two properties, Value and IsValid. The Value property is the value to be validated and the IsValid property is a Boolean used to set the return result of the validation. You can view a client-side validation example in the Validating Form Input Controls section.  For server-side custom validation, place your custom validation in the validator's OnServerValidate delegate. 
<html>
<head>
    <script language="VB" runat=server>

        Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)
            If (Page.IsValid) Then
               lblOutput.Text = "Page is valid!"
            Else
               lblOutput.Text = "Page is not valid! :-("
            End If
        End Sub

    Sub ServerValidate (sender As Object, value As ServerValidateEventArgs)
        Dim num As Integer
            ' even number?
        If Integer.TryParse(value.Value, num) Then
            value.IsValid = (num Mod 2 = 0)
        
        Else 
            value.IsValid = false
        End If
    End Sub

   </script>

</head>
<body>

<h3><font face="Verdana">CustomValidator Example</font></h3>

<form runat="server">

    <asp:Label id=lblOutput runat="server"
        Text="Enter an even number:"
        Font-Names="Verdana"
        Font-Size="10pt" /><br>

    <p>

    <asp:TextBox id=Text1 runat="server" />

    &nbsp&nbsp

    <asp:CustomValidator id="CustomValidator1" runat="server"
        ControlToValidate="Text1"
        OnServerValidate="ServerValidate"
        Display="Static"
        Font-Names="verdana" Font-Size="10pt">
           Not an even number!
    </asp:CustomValidator>

    <p>

    <asp:Button text="Validate" onclick="ValidateBtn_OnClick" runat="server" />

</form>

</body>
</html>






Sunday, January 1, 2012

Tic-Tac-Toe game in Vb.net

Example



Public Class Form1
    Dim n As Integer
    Dim sz As Integer = 40
    Dim px As Integer = sz
    Dim py As Integer = sz
    Dim turn As String = "X"
    Dim l() As Label


    Private Sub Form1_Load(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles MyBase.Load
        
        n = 3
        l = New Label(n * n - 1) {}
        For i As Integer = 0 To n * n - 1
            l(i) = New Label
            l(i).Size = New Size(sz, sz)
            l(i).Location = New Point(px, py)
            px += sz
            If (i + 1) Mod n = 0 Then
                px = sz
                py += sz
            End If
            l(i).AutoSize = False
            l(i).BorderStyle = BorderStyle.FixedSingle
            l(i).Font = New Font("Arial", 20, FontStyle.Bold)
            Me.Controls.Add(l(i))
            Me.Height = (n + 2) * sz + 30
            Me.Width = (n + 2) * sz
            AddHandler l(i).Click, AddressOf lb1_click
        Next
    End Sub


    Public Sub lb1_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim l As Label = CType(sender, Label)
        If l.Text = "" Then
            If turn = "X" Then
                l.Text = "X"
                If checkwin(turn) Then
                    MsgBox(turn + "win")
                    Me.Close()
                End If
                turn = "0"
            Else
                l.Text = "0"
                If checkwin(turn) Then
                    MsgBox(turn + "win")
                    Me.Close()
                End If
                turn = "X"
            End If
            If Draw() Then
                MsgBox("Draw")
                Me.Close()
            End If
        End If
    End Sub


    Private Sub setPosition()
        Dim sc_width As Integer
        Dim sc_height As Integer
        sc_width = Screen.PrimaryScreen.Bounds.Left
        sc_height = Screen.PrimaryScreen.Bounds.Top
        Me.Left = sc_width - (Me.Width / 2) + 350
        Me.Top = sc_height - (Me.Height / 2) + 350
    End Sub


    Public Function checkwin(ByVal turn As String) As Boolean
        Dim cnt As Integer = 0


        For i As Integer = 0 To n - 1
            cnt = 0
            For j As Integer = i * n To (i * n) + n - 1
                If l(j).Text = turn Then
                    cnt += 1
                End If
            Next
            If cnt >= n Then
                Return True
            End If
        Next
        For i As Integer = 0 To n - 1
            cnt = 0
            For j As Integer = 0 To n - 1
                If l(j * n + i).Text = turn Then
                    cnt += 1
                End If
            Next
            If cnt >= n Then
                Return True
            End If
        Next
        cnt = 0
        For i As Integer = 0 To n - 1
            If l(i * (n + 1)).Text = turn Then
                cnt += 1
            End If
        Next
        If cnt >= n Then
            Return True
        End If
        cnt = 0
        For i As Integer = n - 1 To n * (n - 1) Step n - 1
            If l(i).Text = turn Then
                cnt += 1
            End If
        Next
        If cnt >= n Then
            Return True
        End If
        Return False
    End Function


    Public Function Draw() As Boolean
        For i As Integer = 0 To n * n - 1
            If l(i).Text = "" Then
                Return False
            End If
        Next
        Return True
    End Function
End Class