views:

369

answers:

4

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 validate before sending?

+1  A: 

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.

James Black
+1  A: 

just pulling the textarea by it id

i.e.

$('#mytextarea').value();
RageZ
+3  A: 

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>.

Asaph
+1  A: 

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.
        }
    });
});
NawaMan