使用 jquery 验证 <textarea>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3091330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:51:49 来源:igfitidea点击:
Validating a <textarea> with jquery
提问by Rey
i'm trying validate a form (all fields) with jQuery
我正在尝试使用 jQuery 验证表单(所有字段)
My code is valid only for the input area, but it's don't works for the <textarea>
, anybody knows how i can fix this problem?
我的代码仅对输入区域有效,但不适用于<textarea>
,有人知道我如何解决这个问题吗?
$(".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ível.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' />");
});
}
});
return false;
});
})
回答by klox
<script type="text/javascript">
$(document).ready(function () {
$("#submit").validate({
rules:{
textarea_name:{
required:true,
minlength:8
}
}
});
});
</script>