既存のフォームで jquery 検証プラグインを使用していますが、送信時に起動しません。
ブラウザで JavaScript エラーは発生しませんが、何も起こらず、フォームが送信されますが、これはすべきではありません。
コードは次のとおりです。
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js></script>
<script type="text/javascript">
$().ready(function () {
// validate the comment form when it is submitted
$("#registrationform").validate();
// validate signup form on keyup and submit
$("#registrationform").validate({
rules: {
fullname: {
required: true,
minlength: 2,
maxlength: 50
}
},
messages: {
fullname: {
required: "Please enter a name",
minlength: "Your name must consist of at least 2 characters",
maxlength: "Your name must consist of not more than characters"
}
}
});
});
</script>
</head>
<body>
<form id="registrationform" name="registrationform" method="post" action="pageaftervalidation.html">
<table>
<tr>
<td>Name:</td>
<td><input id="fullname" name="fullname" type="text"></td>
</tr>
<tr>
<td colspan="2">
<input type="Submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>
送信時に検証が起動しないのはなぜですか?
問題が見つかりました。
検証関数を 2 つ持つことはできません。デモから、送信時とキーアップ時の両方でフォームを検証しようとしましたが、キーアップの検証では両方が行われるため、その必要はありませんでした。
上記を修正するために、行を削除します
$("#registrationform").validate();