Friday 1 June 2012

Textbox validation in asp.net using javascript


this is textbox validation in asp.net using javascript.
<head runat="server">
    <title>*** Validation ***</title>
    <link href="Styles/Style.css" type="text/css" />
    <script language="javascript" type="text/javascript">
        ////Restricting to enter only characters for name

        function onlyCharacters(event) {
            var charCode = (event.which) ? event.which : event.keyCode
            if ((charCode >= 97) && (charCode <= 122) || (charCode >= 65) && (charCode <= 90))
                return true;

            return false;
        }

        //Restricting to enter only numbers for mobile number

        function onlyNumbers(event) {
            var charCode = (event.which) ? event.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }        

        //validating empty name textbox

        function validate() {
            var name = document.getElementById("txtName").value;
            var len = name.length;
            var nTextbox, eTextbox, mTextbox;
            nTextbox = eTextbox = mTextbox = true;
            if (len == 0) {
                //alert("You must enter a valid name...!!!");
                //return false;
                nTextbox = false;   
            }

            //validating empty email textbox

            var email = document.getElementById("txtEmail").value;
            len = email.length;
            if (len == 0) {
                //alert("You must enter a valid email...!!!");
                //return false;
                eTextbox = false;
            }

            //validating empty mobile textbox

            var mobile = document.getElementById("txtMobile").value;
            len = mobile.length;
            if (len == 0) {
                //alert("You must enter a valid mobile number...!!!");
                mTextbox = false;
            }

            var emptyTextbox = "";
            var count = 0;

            if (!nTextbox) {
                emptyTextbox = emptyTextbox + "Name";
                count = count + 1;
            }
            if (!eTextbox) {
                if (!nTextbox)
                    emptyTextbox = emptyTextbox + ", Email";
                else
                    emptyTextbox = emptyTextbox + "Email";

                count = count + 1;
            }
            if (!mTextbox) {
                if (!nTextbox || !eTextbox)
                    emptyTextbox = emptyTextbox + ", Mobile ";
                else
                    emptyTextbox = emptyTextbox + "Mobile";
                count = count + 1;
            }

            // display error msg

            if (count > 0) {
                if (count > 1) {
                    alert(emptyTextbox + " are mandatory fields...!");
                }
                else {
                    alert(emptyTextbox + " is mandatory fields...!");
                }
                return false;
            }
            
            //validating email format

            var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

            var flag = re.test(email);
            if (!flag) {
                alert("This is not a valid emailID...!!!");
                return false;
            }

            //validating empty mobile number textbox and lenght of the mobile number

            len = mobile.length;
            if (len < 10 || len > 10) {
                alert("Mobile number should have 10 digits...!!!");
                return false;
            }  
                      
            return true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="container">
        Namr:<asp:TextBox ID="txtName" runat="server" onkeypress="return onlyCharacters(event)" ClientIDMode="Static"></asp:TextBox><br />
        Email:<asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static"></asp:TextBox><br />
        Mobile No:<asp:TextBox ID="txtMobile" runat="server" onkeypress="return onlyNumbers(event)" ClientIDMode="Static"></asp:TextBox><br /><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick=" return validate()" />
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment