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>
<html>
<head>
<title>Validating Form</title>
<script>

function validateForm(){
var username = document.forms.form.username.value;
if(username == null || username==""){
alert("Please enter your name");
document.forms.form.username.focus();
return false;
}
var phone = document.forms.form.phone.value;
if(isNaN(phone) || phone.length != 12){
alert("Please enter a valid phone number");
document.forms.form.phone.focus();
return false;
}
var email = document.forms.form.email.value;
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if(atpos < 2 || dotpos-atpos <3){
alert("Please enter a email address");
document.forms.form.email.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<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" />
</form>
</body>
</html>
ARTICLE POSTED BY : Admin

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.