1

私のフォームについて助けを求めることができますか?テキスト フィールドとチェック ボックスの両方を必須フィールドにしたいと考えています。チェック ボックスは検証中です。今必要なのは、テキスト フィールドの検証を要求することだけです。私はジャバスクリプトを使用しています。

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

Doctypeをに変換すると<!DOCTYPE html>、次を使用できます。

required="true"

以下のように実装されます

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

ほとんどのブラウザはHTML5をサポートしています。または、非常に簡単に使用できるjQuery検証を利用します。

于 2012-09-19T11:25:46.390 に答える
0

テキスト フィールドを検証するために、この JS コードを追加します。

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

メール欄も同様。

于 2012-09-19T11:23:37.543 に答える