RSS Feed

Validate a Web Application Form using JavaScript Function

December 5, 2011 by Sourabh Sharma

Hello guys, we have already learned that how to validate a web application form using validation controls. These validation controls are easy to use thats why the developers use these controls. But some time we need to make our web application more fast. As you all know that using more ASP.NET controls makes site more slow to load. So reduce the loading time of the web page we use sometimes JavaScript Function to validate the web form. JavaScript Function are also easy to use and thsi method is very fast to validate the form. and I hope that you will also learn that how to call a Javascript function from the code behind page in ASP.NET.

 


Free Download:
JavaScript Validation : Demo Project

 

 



JavaScript Function of Web Page

<script type="text/javascript">

        function validateForm() {
            var x = document.getElementById('<%= txtName.ClientID %>').value;
            var y = document.getElementById('<%= txtFname.ClientID %>').value;
            if (x == null || x == "") {
                alert("Name must be filled out");
                return false;
            }
            else if (y == null || y == "") {
                alert("Father's name must be filled out");
                return false;
            }
            }
      </script>


Code behind of Web Page


protected void Page_Load(object sender, EventArgs e)
    {
        btnValidate.Attributes.Add("onClick", "return validateForm()");
    }



No Comments »

No comments yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>