How to validate a <textarea></textarea> with Jquery if no form exists?
I knew there is a JQuery form validation, but if there is no form, how to validate with Jquery? Say, there is a
<textarea></textarea>
, I want to send the text via AJAX instead of form. How to valid开发者_如何学Cate before sending?
The <textarea>
tag is not allowed outside a <form>
. What you're proposing wouldn't pass an HTML validator. Just surround your <textarea>
with a <form>
.
Why not just validate it yourself. If there is certain requirements that you want to check, before the submission, or attach an onblur event to the textarea
and you can ensure that the data is correct.
I would opt for the onblur event as that would enable you to let the user know quickly if there is a problem.
just pulling the textarea by it id
i.e.
$('#mytextarea').value();
So you check the value before you send through AJAX.
$('input#mybutton').click(function() {
var text = $('textarea#mytextarea').val();
// Validate the text HERE
$.ajax({
type: "GET",
url: pURL,
async: false,
dataType: "html",
success: function(pReturn) {
// Process the return.
}
});
});
精彩评论