Take the 2-minute tour ×

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

  • Have one <input type="text"> and one <input type="button">
  • If the text-input has exactly N number characters, I want a click on the button to submit the form with one action.
  • Else, the button submits the form with another action.

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

share|improve this question
What do you mean by "action"? The action tag of the form element? –  Tatu Ulmanen Apr 14 '10 at 18:33
Yes, sorry, should've clarified that. :) –  Twisted Intellect Apr 14 '10 at 18:40

4 Answers 4

up vote 2 down vote accepted

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');
        myForm.attr('action', 'http://somewhereelse.com');
share|improve this answer

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

share|improve this answer

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

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

//Put adapted code here

} )


share|improve this answer
$(function() {
            var textLength = $('#text').val();

            if (textLength.length === 10) // 10 for exmaple
                //do bla
            else { 
                // do labla
share|improve this answer

Your Answer


