views:

560

answers:

1

I have a form, and want to disable/enable its submit button depending on whether the form's input text fields are empty or have had text entered into them.

I think this means having an event handler for the input text fields' keydown or keypress events: when it's fired, this event handler should test whether the input text fields contain text, and enable or disable the submit button accordingly.

The change event looks like it ought to be more useful than the keydown or keypress events, except that it isn't fired until the control in question loses the focus, and what good is that: since the user wants to just type something and then click on the submit button, I want an event handler that's fired by text being entered and not only when the control loses focus.

My questions are:

  • Are keydown and/or keypress events fired before or after the corresponding text has been inserted into the input text field?
  • Are keydown and/or keypress events cancellable (can you cancel them to prevent the corresponding text from being entered)?


Edit: FYI, the jQuery validation plug-in re-tests form validity on key up.

+1  A: 

In answer to your questions...

  1. The keydown or keypress events can be intercepted before text is input using javascript
  2. You can return false after binding a function to the keydown or keypress event. This will prevent the text from being input.

Example using jQuery:

$("#inputfield").live("keydown", function(){
    return false;
});

If you want to test some input for value on form submit you can do that using jQuery submit().

$("#theform").submit(function() {
    var formval = $("#theinput").val();
    if(formval == "") { //or some better test
        alert("input value"); //or some other alert...
        return false;
    } else {
        return true;
    }
});

Returning false will invalidate the click to submit the form.

Edit: If as you say in your comments you want to disable the submission of the form until requirements are met for the input field(s)

$("#inputfield").live("keyup", function(){
    if($("#inputfield").val() == "") {
        $('#button').attr("disabled", true); 
    } else {
        $('#button').attr("disabled", false); 
    }
});

You want to use the "keyup" event handler to allow the text to be sent first. see my example: on jsbin

jjclarkson
If they're fired before the text is entered, doesn't that make them useless for the purpose for which I intended to use them (which was to test whether the input fields contain text)? What other mechanism can I use instead (to enable/disable the submit button depending on whether input fields contain text)?
ChrisW
You can put a test in place onsubmit to check if there is something in the input field.
jjclarkson
Doing that would be a little later than I wanted: it's common, in a desktop application at least, to enable/disable a dialog's "OK" button in realtime depending on whether it's valid/clickable. You're saying, instead, to let the button be clickable all the time (so that onsubmit can always be invoked), but to check in onsubmit (and I suppose display an error message to say that a field is required). I realize that's a common/usual way to do it in a browser; I was wondering whether the technology (i.e. behaviour of available events) even supported the other UI.
ChrisW
Sure you can disable it until text is entered. (that will give a visual clue that something is missing.) Just test the input value on every keydown and toggle the disabled attribute: $('#target').attr("disabled", true); whenever your requirements for the field are met.
jjclarkson
But you said that keydown is fired before the text is entered: so testing in keydown would be too early to know whether the input field contains text.
ChrisW
Instead of testing on key down, test onblur. This will get fired when the field loses focus whether the user typed something in or just left it blank
ferrari fan
Test onkeyup. Or onblur as suggested above.
jjclarkson
Again, onblur and/or onchange don't seem to be exactly what I want. I'd like to be able to 1) Position the input focus on an input text field; 2) Press a key, for example 'a'; 3) Therefore see the "OK" button being enabled (without having needed to do something that causes the input text field to lose focus).
ChrisW
Ok I think my example using on keyup fulfills those requirements.
jjclarkson
Is it possible to initiate an asynchronous check? If I were faced with this situation in Windows, I'd use the PostMessage or BeginInvoke APIs. For example, when the keydown handler fires, is there an API to initiate an asynchronous event/method callback, which will be invoked after this event (and its default processing, i.e. updating the input control) has been allowed to complete: for example, is there any way to say "call this other method after some tiny asynchronous delay"?
ChrisW