Blog » JavaScript » javascript validation

javascript validation

In This article will learn you how to create Form validation using HTML and JavaScript. Forms are used in web pages correct information for collects.JavaScript validation is a client side validation it's used by web browser before input filed sent to the web server.In This article we are going to validate username,email and mobile number etc.

complete source code

<!doctype html>
<title>Validating Form</title>

function validateForm(){
var username = document.forms.form.username.value;
if(username == null || username==""){
alert("Please enter your name");
return false;
var phone =;
if(isNaN(phone) || phone.length != 12){
alert("Please enter a valid phone number");;
return false;
var email =;
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if(atpos < 2 || dotpos-atpos <3){
alert("Please enter a email address");;
return false;
return true;
<form name="form" method="post" action="" onsubmit="return validateForm();">
Username: <input type="text" name="username" /><br/>
Phone number: <input type="text" name="phone" /><br/>
Email: <input type="text" name="email" /><br/>
<input type="submit" value="submit" />

Hi, I'am k.g uththara. I'm a professional web developer. I write blog posts in my free time. if you want to contact me, send a message on contact page, thanks.