gpt4 book ai didi

javascript - 对文本字段和复选框进行验证

转载 作者:行者123 更新时间:2023-11-28 09:40:27 24 4
gpt4 key购买 nike

请问我可以就我的表格寻求帮助吗?我想将文本字段和复选框设为必填字段。该复选框正在验证,我现在需要的只是文本字段需要验证。我正在使用 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>

最佳答案

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

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

电子邮件字段也相同。

关于javascript - 对文本字段和复选框进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com