7

I am using the jQuery datepicker on a text input field and I don't want to let the user change any of the text in the text input box with the keyboard.

Here's my code:

$('#rush_needed_by_english').keydown(function() {
  //code to not allow any changes to be made to input field
});

How do I not allow keyboard typing in a text input field using jQuery?

3 Answers 3

11
$('#rush_needed_by_english').keydown(function() {
  //code to not allow any changes to be made to input field
  return false;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Does this cover paste events? What if I click in the box, and type ctrl-V ?
Use keypress instead of keydown. keypress will still allow tab, ctrl-v, etc. etc. while keydown blocks all keys. @Cheeso, I may be 5 years late in answering your question, but.... I answered it!
6

You could simply make the field read-only with:

$('#rush_needed_by_english').attr('readonly', 'readonly');

If you're using jQuery 1.6+, you should use the prop() method instead:

$('#rush_needed_by_english').prop('readOnly', true); 
// careful, the property name string 'readOnly' is case sensitive!

Or ditch jQuery and use plain JavaScript:

document.getElementById('rush_needed_by_english').readOnly = true;

Comments

0

You could:

$('#rush_needed_by_english').attr('disabled', 'disabled');

2 Comments

No, that would make the field look disabled also and I am trying to avoid that. I just dont want them to be able to type.
ANother problem with attribute disabled these fields will not get passed to server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.