这是我的外部JavaScript文件和函数 - validations.js
function validateEmail() {
var x = document.forms["Register"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address");
return false;
}
}
function validate(){
{
var a=document.forms["Register"]["name"].value;
var b=document.forms["Register"]["email"].value;
var c=document.forms["Register"]["password"].value;
//var d=document.forms["Form"]["answer_d"].value;
if (a==null || a=="",b==null || b=="",c==null || c=="")
{
alert("Please Fill All Required Field");
return false;
}
}
}
这是我的HTML文件
<body>
<h3>User Registration Form</h3>
<form name ="Register" onsubmit="return validate() && validateEmail();" method="POST" action="RegisterServlet">
<table align="center" cellpadding = "10">
<tr>
<td>First Name</td>
<td><input type="text" name="name" maxlength="30"/>
(max 30 characters a-z and A-Z)
</td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" maxlength="100" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" maxlength="100" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
<script src="validations.js"></script>
</body>
这是我试过的其他提交代码
onsubmit="return!!(validate() & validateEmail())"
onsubmit="return(validate() && validateEmail())"
onsubmit="return validate() && validateEmail()"
当我将JavaScript放在同一页面时,它正在运行。
答案 0 :(得分:0)