How to Validate ASP.NET control's using JAVA SCRIPT???(For beginner's, works on Visual studio 2010, vs-2012)

How to Validate ASP.NET control's using JAVA SCRIPT???(For beginner's, works on Visual studio 2010, vs-2012)

Hi friends, Today i am going to explain how to validate ASP.NET controls by using JAVA SCRIPT!!!

Here is the sample ASP.NET form i have designed in my Project!!!

Now, after designing this page i have to add Java Script code, which validates the controls of this page!!!
So to do this, I wrote a Javascript function in the head part of the ASP.NET form as given below!!!
<script type="text/javascript">
        function validateForm() 
        {
            var Firstname = document.getElementById('<%=TextBox1.ClientID %>').value;
            var Password = document.getElementById('<%=TextBox2.ClientID %>').value;
            var Email = document.getElementById('<%=TextBox4.ClientID %>').value;
            if (Firstname == "") {
                alert("Enter First Name");
                return false;
            }
            if (Password == "") {
                alert("Enter Password");
                return false;
            }
            if (Email == "") {
                alert("Enter Email");
                return false;
            }
            var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/
            var EmailmatchArray = Email.match(emailPat);
            if (EmailmatchArray == null) {
                alert("Your email address seems incorrect. Please try again.");
                return false;
            }
        }
</script>
Here is the image of the code,

Here i am taking first name from username textbox, Password from password text box, and email id from email textbox.
First I am checking that these field's should not be empty, and after that i am checking that Email must be in valid format. So "emailpat" is a expression which validates that the email must be in valid format.


After doing all these just go to form part and add "on submit = return [function name]()"

Here in this present scenario, it is like.

<form id="form1" runat="server" onsubmit="return validateForm()">




now save all and run the Project!!! :)

Comments

Popular posts from this blog

What is the difference between asynchronous and synchronous execution?

Yahoo accepting requests for inactive email IDs

How to install JDK 1.7.0 and how to set path and classpath variables