Skip to main content

Validate a form using javascript

We should validate a form for null values, otherwise user can click submit button without filling necessary information. In that case, our database will have many null values.

So below is a simple validation using javascript for validating a form.



First you need to create a form with neccessary values as below:

Html Code
Name
Email

Now, you when user will click submit button it will redirect to another page defined in action of form tag. But we want user to fill all the values of the form. So here we are using javascript function in "onsubmit" event as written above. Write your javascript inside header tag of your document.

Javascript Code

function validate(){

    

    var fullname=document.form1.fullname.value;

    var email = document.form1.email.value;

    

    var atpos = email.indexOf("@");

    var dotpos = email.lastIndexOf(".");

    if(fullname==""){

        alert("Enter Full Name");

        return false;

    }

     else if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length) {

        alert("Enter valid e-mail address");

        return false;

    }

    

    else {

        return true;

    }

}

</pre>

In above code, we are validating if the name field is empty or not. Also we are validating email field is empty or not along with if the user is giving valid email address.

Comments

Popular posts from this blog

CSS List

CSS allows you to customize the lists that can be made with HTML  to the extent that even images can be used as bullet points for unordered lists. CSS List Style Type If you want to use something different from the default numbering of ordered lists, or the bullets/discs of unordered lists, then all you have to do is choose a different style for your lists. CSS allows you to select from a wide variety of different list item shapes. Unordered list styles: square, circle, disc (default), and none Ordered list styles: upper-alpha, lower-alpha, upper-roman, lower-roman, decimal (default), and none Code:

Accessible Forms

Forms aren't the easiest of things to use for people with disabilities. Navigating around a page with written content is one thing, hopping between form fields and inputting information is another. Because of this, it is a good idea to add a number of elements to the form. Labels Each form field should have its own label. The label tag sorts this out, with a for attribute that associates it to a form element:

Join in MySQL

Join clause is used to combine rows from two or more tables depending upon common field between them. There are different kinds of Joins. The most common and useful Join is Inner Join. Inner Join: It will return all rows from multiple tables where the condition is met.