Create one form in which username must be 10 characters long, last name must be 5 characters long, validate email address and also validate for nonempty (fields must not be blank) fields.
CODING:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function validateform(){
var name=document.myform.name.value;
var lname=document.myform.lname.value;
var x=document.myform.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (name.length<10){
alert("Input First Name must be 10 char.");
return false;
}
if (lname.length<5 ){
alert("Input Last Name must be 5 char.");
return false;
}
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
alert("Please enter a valid e-mail address. \n atpostion:"+atposition+"\n dotposition:"+dotposition);
return false;
}
}
</script>
</head>
<body>
<form name="myform" onsubmit="return validateform()">
First Name:<input type="text" name="name" placeholder="Your First Name"><br>
Last Name:<input type="text" name="lname" placeholder="Your Last Name"><br>
Email:<input type="text" name="email" placeholder="Example@gmail.com"><br>
<input type="submit" Value="SEND IT!">
<input type="reset" value="CANCEL">
</form>
</body>
</html>
CODING:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function validateform(){
var name=document.myform.name.value;
var lname=document.myform.lname.value;
var x=document.myform.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
if (name.length<10){
alert("Input First Name must be 10 char.");
return false;
}
if (lname.length<5 ){
alert("Input Last Name must be 5 char.");
return false;
}
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
alert("Please enter a valid e-mail address. \n atpostion:"+atposition+"\n dotposition:"+dotposition);
return false;
}
}
</script>
</head>
<body>
<form name="myform" onsubmit="return validateform()">
First Name:<input type="text" name="name" placeholder="Your First Name"><br>
Last Name:<input type="text" name="lname" placeholder="Your Last Name"><br>
Email:<input type="text" name="email" placeholder="Example@gmail.com"><br>
<input type="submit" Value="SEND IT!">
<input type="reset" value="CANCEL">
</form>
</body>
</html>
Comments
Post a Comment