jQuery 验证插件响应消息
我正在使用 jQuery 验证插件创建 JavaScript/ajax 评论表单。
如何自定义发送给用户的消息?目前,当字段留空时,表单会返回启发式类型:NAME_FULL 服务器类型:NO_SERVER_DATA 字段签名:3489289364 表单签名:13828296746807249018 实验 id:""
。
我只想说“请输入您的名字”。这是控制插件和表单的 JavaScript:
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
$(document).ready(function(){
$("#commentForm").submit(function(){
if($("#commentForm").validate()){
$.ajax({
type: 'POST',
url: 'process.php',
data: $(this).serialize(),
success: function(returnedData){
$('#commentForm').append(returnedData);
}
});
}
return false;
});
});
<form class="cmxform" id="commentForm" method="POST" action="">
<fieldset>
<p>
<label for="cname">Name</label>
input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
php 非常简单:
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
print "E-mail is correct";
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]';
mail($to, $subject, $message, $headers);
} else {
print "E-mail is not correct";
}
I am creating a JavaScript / ajax comment form using the jQuery Validation Plugin.
How do I customize the messages that are sent to the user? Currently, the form returns heuristic type: NAME_FULL server type: NO_SERVER_DATA field signature: 3489289364 form signature: 13828296746807249018 experiment id:""
when a field is left blank.
I just want it to say "Please enter your name." Here is the JavaScript that controls the plugin and the form:
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
$(document).ready(function(){
$("#commentForm").submit(function(){
if($("#commentForm").validate()){
$.ajax({
type: 'POST',
url: 'process.php',
data: $(this).serialize(),
success: function(returnedData){
$('#commentForm').append(returnedData);
}
});
}
return false;
});
});
<form class="cmxform" id="commentForm" method="POST" action="">
<fieldset>
<p>
<label for="cname">Name</label>
input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
And the php is pretty straightforward:
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
print "E-mail is correct";
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]';
mail($to, $subject, $message, $headers);
} else {
print "E-mail is not correct";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
验证插件的正确实现...
验证插件文档
EDIT:
Proper implementation of Validation Plugin...
Validation Plugin Documentation