【问题标题】:Validating a <textarea> with jquery使用 jquery 验证 <textarea>
【发布时间】:2010-06-22 16:22:30
【问题描述】:

我正在尝试使用 jQuery 验证表单(所有字段)

我的代码仅对输入区域有效,但不适用于&lt;textarea&gt;,有人知道我该如何解决这个问题吗?

$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();

var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
   return false;
}
var email = $("input#email").val();
 if (email == "") {
  $("label#email_error").show();
  $("input#email").focus();
  return false;
}
var subject = $("input#subject").val();
  if (subject == "") {
  $("label#subject_error").show();
  $("input#subject").focus();
  return false;
}
var message = $("textarea#message").val();
if (message == "") {
  $("label#message_error").show();
  $("textarea#message").focus();
  return false;
}

var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;
//alert (dataString);return false;

$.ajax({
  type: "POST",
  url: "bin/process.php",
  data: dataString,
  success: function() {
    $('#contact_form').html("<div id='message'></div>");
    $('#message').html("<h2>Mensagem enviada com sucesso!</h2>")
    .append("<p>Entraremos em contato assim que poss&iacute;vel.</p>")
    .hide()
    .fadeIn(1500, function() {
      $('#message').append("<img id='checkmark' />");
    });
  }
 });
return false;
});
})

【问题讨论】:

  • 你想做什么?如果提交后全部为空,则显示错误消息?或者什么?

标签: jquery validation


【解决方案1】:
<script type="text/javascript">
        $(document).ready(function () {

        $("#submit").validate({
                                rules:{
                                        textarea_name:{
                                                required:true,
                                                minlength:8
                                                }
                                      }
                               });
                           });
</script>

【讨论】:

  • 您好,感谢您的评论。我上面写的代码是正确的。这是浏览器缓存的问题,无法正常运行。他试图验证表单上的所有字段。谢谢
猜你喜欢
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多