我已经为html表单制作了一个小代码,并试图用javascript验证它,但它不起作用,甚至没有给出任何错误... 我不知道背后的问题是什么.... 我见过[javascript简单验证不起作用?
这是我的代码..
<!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. johndoe@acme.com");
}
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. johndoe@acme.com");
}
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>
答案 0 :(得分:2)
并将method="post"
添加到您的form
代码中。
答案 1 :(得分:1)
在formValidate()
中更改此内容 document.myForm.Name.value == ""
至document.myForm.dob.value == ""
document.myForm.EMail.value == "" to document.myForm.email.value == ""
请勿使用
document.write("<b>" + day + "/" + month + "/" + year + "</b>");
它将取代你身体的全部内容
答案 2 :(得分:1)
首先,在formValidate(),
首先如果条件不正确。您的表单没有“name”元素,它应该是“dob”或“age”或“value”。
其次,在第二个if而不是这个
if( document.myForm.EMail.value == "" )
,应为if( document.myForm.email.value == "" )
。