Monday 5 November 2012

JavaScript Validation code in asp.net .



Here I have  write some validation code in asp.net for  required field , range validator, email validator ,phone validator etc.
Below all codes are given :
Write this code in header :


<script>
    function val() {
        c = /^[a-zA-Z]*$/;
        if (document.getElementById("TextBox1").value.match(c) == null) {
            alert("please enter your uname and in proper formate.");
            return false;
        }
        if (document.getElementById("TextBox2").value == "") {
            alert("please enter your first name.");
            return false;
        }

        if (document.getElementById("TextBox3").value == "") {
            alert("please enter your last name.");
            return false;
        }

        if (document.getElementById("TextBox4").value == "") {
            alert("please inter your email id.");
            return false;
        }
        e = /^[a-z A-Z 0-9_\.\-]\+@(([a-z A-Z 0-9\-])+\.)+([a-z A-Z 0-9]{2,4})+$/;
        if (document.getElementById("TextBox4").value.match(e) == null) {
            alert("please inter your email id in proper formate.");
            return false;
        }
        if (document.getElementById("TextBox5").value == "") {
            alert("please enter your password");
            return false;
        }

        if (document.getElementById("TextBox6").value == "") {

            alert("please enter your cpass");
            return false;
        }
        if (document.getElementById("TextBox7").value == "") {
            alert("please enter your mobile no.");
            return false;
        }
        p = document.getElementById("TextBox7").value;
        if (isNaN(p)) {
            alert("please enter your mobile no in digit.");

            return false;
        }
        if (document.getElementById("TextBox7").value.length < 10) {
            alert("mobile no must be ten digit no.");
            return false;
        }
        if (document.getElementById("TextBox7").value.length > 10) {
            alert("mobile no must be ten digit no.");
            return false;
        }
        if (document.getElementById("TextBox8").value == "") {
            alert("please enter your date of birth.");
            return false;
        }
        if (document.getElementById("RadioButtonList1").value == "") {
            alert("please enter your cpass");
            return false;
        }
    }
</script>

After this you can call this function  on control where you want .
For example:
<asp:TextBox ID="TextBox1" runat="server" onkeyup="reurn val();"></asp:TextBox>

If any query regarding this post feel free to ask or send a mail.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.