Sunday 21 July 2013

Validation in ASP.NET using JAVA SCRIPT Or Restrict the Length of Control

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

<!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>
   
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <script type="text/javascript">
    function validate() {
        var textbox = document.getElementById('<% = TextBox1.ClientID %>').value;
        var drop = document.getElementById('<%=DropDownList1.ClientID %>').value;
        if (textbox == "") {
            alert("enter the value");
document.getElementById('<% = TextBox1.ClientID %>').focus();
            return false;
        }
        if (drop == "Select") {
            alert("selectany value ");
            return false;
        }
    }
</script>
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>yes</asp:ListItem>
            <asp:ListItem>no</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Fore more visit http://www.codeproject.com/Tips/404274/Client-Side-Validation-using-JavaScript
If u want to restrict the length then use

  var drop = document.getElementById('<%=DropDownList1.ClientID %>').value.Length

then compare length to any number 

No comments:

Post a Comment