Reputation: 165
i have made a small code for html form and trying to validate it with javascript but it is not working and not even giving any error... I am not getting whats the problem behind.... i have seen [javascript simple validation not working?
here is my code..
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A simple Input Form</title>
<script type="text/javascript">
function validateEmail() {
//var emailID = document.myForm.email.value;
if(!validateNonEmpty(inputField, helpText))
return false;
return validateRegEx(/^\d{2}\/\d{2}\/\d{4}$/, inputField.value, helpText,
"please enter an email address i.e. [email protected]");
}
function validatedob(inputField, helpText){
if(!validateNonEmpty(inputField, helpText))
return false;
return validateRegEx(/^\d{2}\/\d{2}\/\d{4}$/, inputField.value, helpText,
"please enter an email address i.e. [email protected]");
}
function age(){
var currentDate = new Date()
var day = currentDate.getDay()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
}
function formValidate(){
if( document.myForm.dob.value == "" )
{
alert( "Please provide your date of birth!" );
document.myForm.dob.focus() ;
return false;
}
else {
var dateformat = validatedate();
if (dateformat == false)
{
return false;
}
}
if( document.myForm.email.value == "" )
{
alert( "Please provide your Email!" );
document.myForm.email.focus() ;
return false;
}else{
var ret = validateEmail();
if( ret == false )
{
return false;
}
}
if (document.myForm.age.value="")
{
alert("please provide your age");
document.myForm.age.focus();
return false;
}
else
{
var ageformat = validateage();
if (ageformat == false)
{
return false;
}
}
}
</script>
</head>
<body>
<div id="left">
<input type="button" value="previous">
<img src="/images/jump2" alt="Just-in-time" />
</div>
<input type="button" value="next">
<form name="myForm" method="post" action="success.html" onsubmit="formValidate();">
Date of Birth: <input id="dob" type="text" name="dob" size=10><br>
Age: <input id="age" type="text" name="age"><br>
Email: <input id="email" type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Upvotes: 1
Views: 216
Reputation: 827
First of all,in your formValidate(),
First if condition is incorrect.Your form does not have a "name" element, it should either be "dob" or "age" or "value".
Secondly, in the second if instead of this
if( document.myForm.EMail.value == "" )
, it should be if( document.myForm.email.value == "" )
.
Upvotes: 1
Reputation: 4775
Change this in formValidate()
document.myForm.Name.value == ""
to document.myForm.dob.value == ""
document.myForm.EMail.value == "" to document.myForm.email.value == ""
Do not use
document.write("<b>" + day + "/" + month + "/" + year + "</b>");
It will replace the whole contents of your body
Upvotes: 1