Twisted Intellect
Twisted Intellect

Reputation: 25

Submit a different form depending on character count in jQuery?

Ok, so here's what I want to do, preferably with jQuery 1.4.2:

Unfortunately I'm a jQuery n00b, and I can't get it to work… ._.

Upvotes: 1

Views: 339

Answers (4)

Ali Habibzadeh
Ali Habibzadeh

Reputation: 11568

$(function() {
        $('#button').click(function() {
            var textLength = $('#text').val();

            if (textLength.length === 10) // 10 for exmaple
            {
                //do bla
            }
            else { 
                // do labla
            }
        });
    });

Upvotes: 0

Abs
Abs

Reputation: 57956

This questions chosen answer should help. Place that code (once you have adapted) below:

$("myFormID").submit( function () {

//Put adapted code here

} )

;

Upvotes: 0

Aaron McAdam
Aaron McAdam

Reputation: 706

To get the number of characters, do $('input[type=text]').val().length

Upvotes: 0

Andy E
Andy E

Reputation: 344695

If you use a <button type="submit">, you could handle the form's submit event and change the action accordingly, so that the submit directs to the page you want. This should be the easiest way, with minimal code necessary -- something like:

var myForm = $('#myForm');
myForm.submit(function ()
{
    if ($('#myInput').val().length == 42)
        myForm.attr('action', 'http://somewhere.com');
    else
        myForm.attr('action', 'http://somewhereelse.com');
}

Upvotes: 2

Related Questions