<script type="text/javascript">
function validateForm()
{
var x=document.forms["frm"]["iam"];
if (x.value=="")
{
document.getElementById('erroriam').innerHTML="Please select.";
x.focus();
return false;
}
document.getElementById('erroriam').innerHTML='';
var x = document.forms['frm']['datepicker'];
if(x.value=="")
{
document.getElementById('errordob').innerHTML="Please select your DateOfBirth";
x.focus();
return false;
}
var x=document.forms["frm"]["country"];
if (x.value=="")
{
document.getElementById('errorcountry').innerHTML="Please select Country.";
x.focus();
return false;
}
document.getElementById('erroriam').innerHTML='';
x=document.frm.pcode;
if(x.value=="")
{
x.value="";
document.getElementById('errorpcode').innerHTML="Please enter the postcode.";
x.focus();
return false;
}
else if(isNaN(x.value))
{
x.value="";
document.getElementById('errorpcode').innerHTML="Please enter only digits.";
x.focus();
return false;
}
else if(x.value.length!=6)
{
x.value="";
document.getElementById('errorpcode').innerHTML="Please enter only 6 digits.";
x.focus();
return false;
}
document.getElementById('errorpcode').innerHTML="";
var emailpattern =/^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/
/*if (document.frm.email.value == "")
{
document.getElementById('erroremail').innerHTML = "Please enter your email.";
return false;
}*/
x=document.frm.email;
if(x.value=="")
{
x.value="";
document.getElementById('erroremail').innerHTML="Please Enter Your Email.";
x.focus();
return false;
}
else if(!emailpattern.test(x.value))
{
x.value="";
document.getElementById('erroremail').innerHTML="Please enter a valid email address.";
x.focus();
return false;
}
document.getElementById('erroremail').innerHTML="";
var x=document.forms["frm"]["uname"];
if (x.value=="")
{
document.getElementById('erroruname').innerHTML="Please enter the first name.";
x.focus();
return false;
}
else if(x.value.length>20)
{
x.value="";
document.getElementById('erroruname').innerHTML="Please enter less than 20 characters.";
x.focus();
return false;
}
else if ((!namepattern.test(x.value)))
{
document.getElementById('erroruname').innerHTML="Please enter only alphabets.";
x.value="";
x.focus();
return false;
}
<h4> <font color="#d26e8e">Register for free to meet new people and start chatting and dating today :)</font></h4>
<div class="register">
<div class="text-slider">
<!--======= INPUT NAME =========-->
<li class="col-sm-12">
<center><h4 style="color:red;"><?php echo isset($_GET["msg"])?$_GET["msg"]:"";?></h4></center>
<label>I am</label>
<div class="form-group">
<select id="iam" name="iam">
<option value="">Select</option>
<option value="a woman seeking a man">a woman seeking a man</option>
<option value="a man seeking a woman">a man seeking a woman</option>
<option value="a woman seeking a woman">a woman seeking a woman</option>
<option value="a man seeking a man">a man seeking a man</option>
</select><p id="erroriam" style="color:red;"></p>
</div>
</li>
<!--======= INPUT PHONE NUMBER =========-->
<li class="col-sm-6">
<label>I was born on</label><p id="errordob" style="color:red;"></p>
<div class="form-group">
<input type="text" name="datepicker" class="form-control" id="datepicker" placeholder="DD/MM/YY">
</div>
</li>
<div class="clearfix"> </div>
<a class="news-letter" href="#">
<label class="checkbox"><input type="checkbox" name="checkbox1" unchecked="" value="yes"><i> </i>I certify that I am over 18 and have read and accepted the terms & conditions, the privacy policy and the cookie policy.</label></a>
<a class="news-letter" href="#">
<label class="checkbox"><input type="checkbox" name="checkbox2" unchecked="" value="yes"><i> </i>Yes, I'd like to receive email offers from partners of Aimer world.</label></a>
var namepattern=/^[a-zA-Z]+$/
var phonepattern = /^\d{10}$/
var emailpattern =/^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/
var emailpattern2 =/^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*$/
var idpattern=/^\d{6}$/;
function validateForm()
{
var x=document.forms["form1"]["firstname"];
if (x.value=="")
{
document.getElementById('pointfn').innerHTML="Please enter the first name.";
x.focus();
return false;
}
else if(x.value.length>20)
{
x.value="";
document.getElementById('pointfn').innerHTML="Please enter less than 20 characters.";
x.focus();
return false;
}
else if ((!namepattern.test(x.value)))
{
document.getElementById('pointfn').innerHTML="Please enter only alphabets.";
x.value="";
x.focus();
return false;
}
document.getElementById('pointfn').innerHTML='';
x=document.forms["form1"]["lastname"];
if(x.value=="")
{
document.getElementById('pointln').innerHTML="Please enter the Last name.";
x.focus();
return false;
}
else if(x.value.length>20)
{
x.value="";
document.getElementById('pointln').innerHTML="Please enter less than 20 characters.";
x.focus();
return false;
}
else if (!namepattern.test(x.value))
{
x.value="";
document.getElementById('pointln').innerHTML="Please enter only alphabets.";
x.focus();
return false;
}
document.getElementById('pointln').innerHTML="";
if((document.form1.gender[0].checked==false)&&(document.form1.gender[1].checked==false))
{
document.form1.gender[0].focus();
document.getElementById('pointgendr').innerHTML='Please select a gender.';
return false;
}
document.getElementById('pointgendr').innerHTML='';
var dd=document.form1.dd.value;
var mmm=document.form1.mmm.value;
var yyyy=document.form1.yyyy.value;
if(!validdate(dd,mmm,yyyy))
{
document.getElementById('pointdob').innerHTML="Plaese select a valid date.";
return false;
}
x.value="";
document.getElementById('pointcontct').innerHTML="Please enter the contact number.";
x.focus();
return false;
}
else if(isNaN(x.value))
{
x.value="";
document.getElementById('pointcontct').innerHTML="Please enter only digits.";
x.focus();
return false;
}
else if(x.value.length!=10)
{
x.value="";
document.getElementById('pointcontct').innerHTML="Please enter only 10 digits.(Mobile number)";
x.focus();
return false;
}
else if(!phonepattern.test(x.value))
{
x.value="";
document.getElementById('pointcontct').innerHTML="Please enter a valid contact number.";
x.focus();
return false;
}
document.getElementById('pointcontct').innerHTML="";
x=document.form1.email;
if(x.value=="")
{
x.value="";
document.getElementById('pointemail').innerHTML="Please enter the email id.";
x.focus();
return false;
}
else if(!emailpattern.test(x.value))
{
x.value="";
document.getElementById('pointemail').innerHTML="Please enter a valid email address.";
x.focus();
return false;
}
document.getElementById('pointemail').innerHTML="";
return confirm("Do you want to submit the form?");
}
function validdate(dd,mm,yyyy)
{
var dateobj = new Date(yyyy, mm, dd);
var datecurrent= new Date();
if((dateobj.getMonth()!=mm)||(dateobj.getDate()!=dd)||(dateobj.getFullYear()!=yyyy)||(dateobj>datecurrent))
{
return false;
}
return true;
}
function confirmreset()
{
if(confirm("Do you want to reset the form?"))
{
document.getElementById('pointfn').innerHTML="";
document.getElementById('pointln').innerHTML="";
document.getElementById('pointgendr').innerHTML="";
document.getElementById('pointdob').innerHTML="";
document.getElementById('pointadrs').innerHTML="";
document.getElementById('pointcontct').innerHTML="";
document.getElementById('pointemail').innerHTML="";
return true;
}
else
return false;
}
validation without submit button.
File name : index.php
<html>
<head>
<script>
function validateform()
{
var x = document.getElementById("first").value;
if(x==null || x =="")
{
document.getElementById("fname").innerHTML="please enter first name";
}
var y = document.getElementById("lastname").value;
if(y==null || y =="")
{
document.getElementById("last").innerHTML="enter your last name";
}
}
<html>
<head>
<meta charset="utf-8">
<title>Validation using JavaScript</title>
<script type="text/javascript">
var hasFocus = false;
function checkName(form)
{
var eobj=document.getElementById('realnameerror');
var sRealName = form.realname.value;
var oRE = /^[a-z0-9]+[_.-]?[a-z0-9]+$/i;
var error=false;
eobj.innerHTML='';
if (sRealName == '') {
error='Error: Username cannot be blank!';
}
else if (sRealName.length < 4)
{
error="UserName should be atleast 4 characters long";
}
else if (!oRE.test(sRealName))
{
error="Incorrect format.";
}
if (error)
{
if (hasFocus == false) {
form.realname.focus();
hasFocus = true;
}
eobj.innerHTML=error;
return false;
}
return true;
}
function checkEmail(form) /* for email validation */
{
var eobj=document.getElementById('emailerror');
eobj.innerHTML='';
var error = false;
if (form.email.value.length == 0) {
error = 'Please enter email.';
} else if (/^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$/.test(form.email.value))
{
return true;
} else {
error = 'Invalid E-mail Address! Please re-enter.';
}
if (error) {
eobj.innerHTML=error;
if (!hasFocus) {
form.email.focus();
hasFocus = true;
}
return false;
}
return true;
}
function validatePwd(form) /* password & retype-password verification */
{
var eobj1=document.getElementById('passworderror');
var eobj2=document.getElementById('password2error');
var minLength=6;
var invalid=' ';
var pw1=form.password.value;
var pw2=form.password2.value;
var error=false;
eobj1.innerHTML='';
eobj2.innerHTML='';
if (pw1.length<1)
{
error='Please enter your password.';
}
else if (pw1.length < minLength)
{
error='Your password must be at least ' + minLength + ' characters long. Try again.';
}
else if (pw1.indexOf(invalid) > -1)
{
error='Sorry, spaces are not allowed.';
} else if (pw2.length == 0) {
error='Please retype password.';
if (!hasFocus) {
form.password2.focus();
hasFocus = true;
}
eobj2.innerHTML=error;
return false;
}
if (error)
{
if (!hasFocus) {
form.password.focus();
hasFocus = true;
}
eobj1.innerHTML=error;
return false
}
if (pw1 != pw2)
{
eobj2.innerHTML=' passwords not matching.Please re-enter your password.';
if (!hasFocus) {
form.password2.focus();
hasFocus = true;
}
return false;
}
return true;
}
function validPhone(form) /* phone no validation */
{
var eobj=document.getElementById('phonenoerror');
var valid = '0123456789';
var phone = form.phoneno.value;
var error=false;
var i=0;
var temp;
eobj.innerHTML='';
if (phone == '')
{
error='This field is required. Please enter phone number';
}
else if (!phone.length > 1 || phone.length < 10)
{
error='Invalid phone number length! Please try again.';
}
else
{
for (i=0; i < phone.length; i++)
{
temp = '' + phone.substring(i, i + 1);
if (valid.indexOf(temp) == -1)
{
error='Invalid characters in your phone. Please try again.';
}
}
}
if (error)
{
if (!hasFocus) {
form.phoneno.focus();
hasFocus = true;
}
eobj.innerHTML=error;
return false;
}
return true;
}
function validate()
{
hasFocus = false;
var form = document.forms['form'];
var ary=[checkName,checkEmail,validatePwd,validPhone];
var rtn=true;
var z0=0;
for (var z0=0;z0<ary.length;z0++)
{
if (!ary[z0](form))
{
rtn=false;
}
}
return rtn;
}
</script>
</head>
<body>