簡體   English   中英

驗證文本字段和復選框

[英]Making validation for both text field and checkbox

請給我表格幫助。 我想同時輸入文本字段和復選框必填字段。 復選框正在驗證,我現在只需要文本字段需要驗證。 我正在使用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";
}

電子郵件字段也相同。

如果您將doctype轉換為<!DOCTYPE html>則可以使用

required="true"

它將實現如下

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

大多數瀏覽器都支持HTML5。 或者,使用相當簡單的jQuery驗證

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM