Steven
Steven

Reputation: 25284

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

Upvotes: 0

Views: 4614

Answers (4)

NawaMan
NawaMan

Reputation: 25687

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

Upvotes: 1

Asaph
Asaph

Reputation: 162771

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

Upvotes: 4

RageZ
RageZ

Reputation: 27313

just pulling the textarea by it id

i.e.

$('#mytextarea').value();

Upvotes: 1

James Black
James Black

Reputation: 41858

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.

Upvotes: 1

Related Questions