简体   繁体   English

如何在 textarea 中使用 jQuery 验证插件?

[英]How to use jQuery Validation Plugin with textarea?

I am having a little bit of trouble understanding how to implement jQuery Validation Plugin .我在理解如何实现jQuery Validation Plugin时遇到了一些麻烦。 With my form that is being submitted with Ajax:使用 Ajax 提交的表单:

$(document).delegate("'#submit-quote'", "submit", function(){
        var quoteVal = $(this).find('[name="quote"]').val();
        $.post("add.php", $(this).serialize(), function(data) {
            var like = $('.quote-wrap span iframe');
            $('.inner').prepend('<div id="'  + data + ' " class="quote-wrap group">' + like + '<div class="quote"><p>' + quoteVal+ '</p></div></div>');
            // console.log("success");
            var id = parseInt(data);
            console.log(id)

// some code after being successfully sent

});

After adding validation plugin, how would I set the defaults and make sure it's what I want the form to do?添加验证插件后,我将如何设置默认值并确保它是我想要表单执行的操作?

It's just a textarea, that a user submits a simple quote.它只是一个文本区域,用户提交一个简单的报价。 (text and numbers) and shouldn't submit anything else. (文本和数字),不应提交任何其他内容。 Right now I can submit HTML, or anything else I like.现在我可以提交 HTML 或任何我喜欢的东西。

What do I need to do to make sure the user can't?我需要做什么来确保用户不能?

I have tried:我努力了:

If all you need to do is make sure users can't post HTML, then check out this article on StackOverflow: HTML-encoding lost when attribute read from input field .如果您需要做的就是确保用户无法发布 HTML,那么请查看 StackOverflow 上的这篇文章: HTML-encoding lost when attribute read from input field

Otherwise, you can use jQuery Validation to perform a similar method during validation.否则,您可以使用 jQuery Validation 在验证期间执行类似的方法。

try this试试这个

$("#myform").validate();
$("a.check").click(function () {
    if ($("#myform").valid()) {
        //if valid do form submit
    }
    else {
        return false;
    }
});

you can check the validity of the form .你可以检查表格的有效性。 if valid do your work .如果有效,请做您的工作。 else return false,否则返回假,

check this检查这个

http://docs.jquery.com/Plugins/Validation/valid http://docs.jquery.com/Plugins/Validation/valid

and one more thing, you are using submit event of the form.还有一件事,您正在使用表单的submit事件。 that causes page refresh.这会导致页面刷新。 so you have to所以你必须

return false OR use http://api.jquery.com/event.preventDefault/返回 false 或使用http://api.jquery.com/event.preventDefault/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM