1

请问我可以就我的表格寻求帮助吗?我想让文本字段和复选框都成为必填字段。复选框正在验证,我现在需要的只是文本字段需要验证。我正在使用 JavaScript。

HTML:

<form name="form" method="post" action="result.php" onSubmit="return checkme()">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Name: </td>
<td> <input name="Name" type="text" id="Name" size="20"></td>
</tr>
<tr>
<td>Email: </td>
<td> <input name="Email" type="text" id="Email" size="20"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="checkbox" name="agree" value="agree_terms"> I agree to the terms</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Submit">      <input type="reset" name="reset" value="Clear"></td>
</tr>
</table>
</form>

Javascript:

<script language="JavaScript" type="text/JavaScript">
<!--//hide script
function checkme() {
missinginfo = "";
if (!document.form.agree.checked) {
missinginfo += "\n - You must agree to the terms";
}
if (missinginfo != "") {
missinginfo ="__________________________________\n" +
"Required information is missing: \n" +
missinginfo + "\n__________________________________" +
"\nPlease complete and resubmit.";
alert(missinginfo);
return false;
}
else {
return true;
}
}

</script>
4

2 回答 2

0

添加此 JS 代码以验证文本字段

if(document.form.Name.value==""){
missinginfo += "Required field";
}

电子邮件字段也一样。

于 2012-09-19T11:23:37.543 回答
0

如果您将 doctype 转换为,<!DOCTYPE html>则可以使用

required="true"

执行如下

<input name="Name" type="text" id="Name" size="20" required="true">

大多数浏览器都支持 HTML5。或者使用相当简单的jQuery 验证。

于 2012-09-19T11:25:46.390 回答