[Solved] Try New Client side JavaScript validation in asp.net c#

 JavaScript validation in asp.net

In this tutorial, we will discuss Client-side JavaScript validation in asp.net c#  we will also see how JavaScript validation work, basically if someone asks why you should use JavaScript validation the answer will be "because it is client-side scripting language". 

javascript validation in asp.net


JavaScript validation is the process of ensuring that user input is correct, clean and useful. All these things we can check before submitting data to the server. That's why we called it client-side validation.

Here you will see how to validate dropdown, radio button, phone no. and date of birth control.

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

<!DOCTYPE html>

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

<head id="Head1" runat="server">

    <title>Vallidations Page</title>

    <script type="text/javascript">

        function Allvalidate() {

            var Validatectrl = "";

            Validatectrl += validateName();

            Validatectrl += validateEmail();

            Validatectrl += validatePhone();

            Validatectrl += validateBirthday();

            Validatectrl += validateGender();

            Validatectrl += validateCountry();

            if (Validatectrl != "") {

                alert(Validatectrl);

                return false;

            }

            else {

                alert("Data Saved"); // you can call here web service insert function

                return true;

            }

        }

        function validatePhone() {

            var get_id;

            var ctrlid = document.getElementById("<%=txtcontct.ClientID %>");

            get_id = ctrlid.value;

            var val;

            val = /^[0-9]+$/;

            var digits = /\d(10)/;

            if (get_id == "") {

                return ("Please Enter PhoneNo" + "\n");

            }

            else if (val.test(get_id)) {

                return "";

            }

            else {

                return ("PhoneNo should be only in digits" + "\n");

            }

        }

        function validateName() {

            var get_id;

            var ctrlid = document.getElementById("<%=txtname.ClientID %>");

            get_id = ctrlid.value;

            var val = /^[a-zA-Z ]+$/

            if (get_id == "") {

                return ("Please Enter Name" + "\n");

            }

            else if (val.test(get_id)) {

                return "";

            }

            else {

                return ("Name accepts only spaces and charcters" + "\n");

            }

        }

        function validateEmail() {

            var get_id;

            var ctrlid = document.getElementById("<%=txtemail.ClientID %>");

            get_id = ctrlid.value;

            var val = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

            if (get_id == "") {

                return ("Please Enter Email Id" + "\n");

            }

            else if (val.test(get_id)) {

                return "";

            }

            else {

                return ("Email format should be: [email protected]" + "\n");

            }

        }

        function validateBirthday() {

            var get_id;

            var controlId = document.getElementById("<%=txtBirthDate.ClientID %>");

            get_id = controlId.value;

            var val = /^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;

            if (get_id == "") {

                return ("Please Enter Dob" + "\n");

            }

            else if (val.test(get_id)) {

                return "";

            }

            else {

                return ("BirthDate format should be: MM-DD-YYYY" + "\n");

            }

        }

        function validateGender() {

            var rbutton = document.getElementById("<%=rdo_gender.ClientID%>");

            var radio = rbutton.getElementsByTagName("input");

            var isChecked = false;

            for (var i = 0; i < radio.length; i++)

            {

                if (radio[i].checked == true) {

                    return "";

                    break;

                }

            }

            if (!isChecked) {

                return ("Please select an Gender " + "\n");

            }

        }

        function validateCountry() {

            var get_id;

            var ctry = document.getElementById("<%=ddl_country.ClientID%>");

            get_id = ctry.value;

            if (get_id == "")

            {

                return ("Please Enter Name" + "\n");

            }

        }

    </script>

</head>

<body>

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

        <h3>Javascript validation Example</h3>

        <table>

            <tr>

                <td>

                    <asp:Label ID="lblname" runat="server" Text="Name"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="lblemail" runat="server" Text="Email"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>

                    <asp:Label ID="lblCntno" runat="server" Text="Phone No"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="txtcontct" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>BirthDate</td>

                <td>

                    <asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox>

                </td>

            </tr>

            <tr>

                <td>Gender</td>

                <td>

                    <asp:RadioButtonList ID="rdo_gender" RepeatDirection="Horizontal" runat="server">

                        <asp:ListItem Value="1">Male</asp:ListItem>

                        <asp:ListItem Value="2">FeMale</asp:ListItem>

                    </asp:RadioButtonList>

                </td>

            </tr>

            <tr>

                <td>Country</td>

                <td>

                    <asp:DropDownList ID="ddl_country" runat="server" AutoPostBack="True">

                        <asp:ListItem  Value="0">Select</asp:ListItem>

                        <asp:ListItem  Value="1">India</asp:ListItem>

                        <asp:ListItem  Value="2">Us</asp:ListItem>

                    </asp:DropDownList>

                </td>

            </tr>

            <tr>

                <td></td>

                <td>

                    <asp:Button ID="bttnsubmit" runat="server" Text="Submit" OnClientClick="javascript:Allvalidate()" Font-Bold="True" />

                </td>

            </tr>

        </table>

    </form>

</body>

</html>


If you are unable to implement JavaScript validation in asp.net, feel free to comment. We will happy to help you.


2 Comments

Post a Comment

Post a Comment

Previous Post Next Post