validating textfield input with ajax on every key entry I'm new to Drupal and Javascript, so please bear with me. I have a simple class project to create custom validation on textfields without using select options or checkboxes. For one of the fields, the only valid inputs are 'right' or 'left'. 

I have validation upon submit working, but I wanted to use ajax to check user input key by key, and display an error message before clicking the submit button. (As soon as it's clear the input will not be 'right' or 'left').

The function is working, but drupal pauses for a half second or so with every keyup while it does the ajax call, making it not very user friendly or practical to use. Hitting backspace to clear the field before drupal 'resumes' even navigates away from the page.

I have looked extensively online for resources on this, and have not found an example or module that seems to address this particular issue. Modifying autocomplete_path seemed like the best option, but I can't figure out how to modify it for my purpose.

I have the module on my github here, or here are the parts in my module dealing with ajax.
In my form:

$form['direction'] = array(
    '#title' =&gt; 'Direction',
    '#type' =&gt; 'textfield',
    '#description' =&gt; 'Enter the shift direction',
    '#element_validate' =&gt; array('validate_direction'),
    '#prefix' =&gt; '&lt;div id="error_message"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#required' =&gt; TRUE,
    '#ajax' =&gt; array(
            'event' =&gt; 'keyup',
            'callback' =&gt; 'validate_direction_callback',
    ), 
);


and then the actual ajax callback

function validate_direction_callback($form, &amp;$form_state){
    $commands = array();
    $commands[] = ajax_command_html('#error_message', render($form['direction']));
    $commands[] = array(
        'command' =&gt; 'validateDirectionCallback',
        'selectedValue' =&gt; $form_state['values']['direction'],
    );
    return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);
}


I didn't include the js file here because it didn't affect the pausing even when it was commented out. Thank You! Oh, and I am using Drupal 7.39.
The ajax callback in drupal is nearly as slow as a complete page load and this takes between 200 und 1000 ms. So checking every key stroke is no option.

If you don't check against the database, there is no need for an ajax callback. I would check "right" or "left" in javascript. If you need a server side check, you can write a stand-alone php file, that does this check without drupal, then your response time will be between 10 and 20 ms.

To replace the slow ajax with javascript you can start with this script:

val_left_or_right.js:

(function ($) {
  $('#edit-direction').keyup(function(){
    var direction_text = $('#edit-direction').val();
    if (direction_text != 'left' &amp;&amp; direction_text != 'right') {
      $('#error_message').html('Please type left or right');
    } else {
      $('#error_message').html('OK');
    }
  });
})(jQuery);


attach the js to the form:

$form['#attached']['js'] = array(
  drupal_get_path('module', 'your_module') . '/js/val_left_or_right.js',
);