×

Html & Html5

it training classes Logo

The World's best Software Development Study Portal

JavaScript Form Validation Example



Javascript provides the facility to validate the form on the client-side data processing will be faster than server-side validation. Most web developers prefer Javascript form validation.

You will create a simple form with two input fields: name,password, and confirm name,password. When you click the submit without filling anything or filling incorrect data format, the form will show error messages.

<script>

function form(){

var name=document.myform.name.value;

var password=document.myform.password.value;

if (name==null || name==""){

alert("Name please");

return false;

}else if(password.length<8){

alert("Password must be at least 8 characters long.");

return false;

}

}

</script>

<form name="myform" method="post" action="#" onsubmit="return form()" >

Name: <input type="text" name="name">

Password: <input type="password" name="password">

<input type="submit" value="register">

</form>









JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute