Disable some characters from input field

prevent unwanted characters in input
html input restrict characters
prevent special characters in input
prevent letters in input field javascript
how to restrict special characters in textbox using javascript
disable special characters in input jquery
how to restrict characters in textbox using javascript
input pattern

I have found some instructions similar, but none helps, all of them are either for special chars or for digits.

I need to make user type only 1 , 2 , 3, 4, 5, N, O, A, B, C . all other characters should be restricted.

Any way I can make my own selection of restricted/allowed chars for inputs ?

  • Not very familiar with javascript.

Try this

    if(e.which == 97 || e.which == 98 || e.which == 99 || e.which == 110 || e.which == 111 || e.which == 65 || e.which == 66 || e.which == 67 || e.which == 78 || e.which == 79 || e.which == 49 || e.which == 50 || e.which == 51 || e.which == 52 || e.which == 53){
    } else {
      return false;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='txt' value='' onpaste="return false" />

Prevent unwanted characters in <input> | by MelkorNemesis, I needed to create an that would accept only numbers and a slash symbol (/) - because pattern for birth number in Czech republic is xxxxxx/xxxx� A disabled input element is unusable and un-clickable. The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

with JQuery,

$("input").keypress( function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;

without JQuery

document.getElementById("foo").onkeypress = function(e) {
    var chr = String.fromCharCode(e.which);
    if ("12345NOABC".indexOf(chr) < 0)
        return false;

For one liners, from @mplungjan and @matthew-lock's comment

document.querySelector("#foo").onkeypress = function(e) {
    return "12345NOABC".indexOf(String.fromCharCode(e.which)) >= 0;

How to Restrict Special Characters From a Form Field, Now we add the Javascript to prevent characters from being typed into the textbox: $('input[type="text"]'). keydown(function(e){ var ingnore_key_codes = [8,32]; if ($. inArray(e. 'Remove spaces and special characters from a string 'Source: Dave Hargis, Microsoft Access MVP (klatuu) Dim lngCtr As Long Dim intChar As Integer. For lngCtr = 1 To Len(strString) intChar = Asc(Mid(strString, lngCtr, 1)) If intChar >= 48 And intChar <= 57 Or _ intChar >= 97 And intChar <= 122 Or _ intChar >= 65 And intChar <= 90 Then

It's not 100% clear whether you want to restrict the form being submitted with invalid values, or literally prevent the user from even typing those values. The other answers deal with the latter, and I think that is what you meant, but there will be people arriving here (like me) who just want to prevent form submission.

In which case:

Use the pattern attribute on the input element:

<input pattern="[REGEX HERE]">



Preventing Character Input Using Javascript, Hi There, I came across this scenario where we prevent users to enter special characters in input text field, and below is the one exception 1. the user can enter � To disable the autocomplete of text in forms, use the autocomplete attribute of <input> and <form> elements. You'll need the "off" value of this attribute. This can be done in a <form> for a complete form or for specific <input> elements: Add autocomplete="off" onto the <form> element to disable autocomplete for the entire form.

If you want to disable a few characters from input field you can do something like that:

<input type="text" onkeydown="return (event.keyCode!=86);"/>

86 is the code for V. Check code for other keys from the following link.


how to prevent a user from entering special characters in input text , If a field does not require a specific type value like – number, character, special character then stop the user from entering. In this tutorial, I show� The <input> tag specifies an input field where the user can enter data. The <input> element is the most important form element. The <input> element can be displayed in several ways, depending on the type attribute. The different input types are as follows: <input type="button"> <input type="checkbox"> <input type="color"> <input type="date">


<input type="text" name="" value="" id="testr">
<script type="text/javascript" src="testkeydown.js"></script>


document.getElementById('testr').addEventListener('keydown', function(e) {
    const regex = RegExp('[0-9a-zA-Z]');

    if (!regex.test(e.key) && e.key != 'backspace') {

In the code example above here you see a piece of code that disables most characters that are not between 0-9, a-z, A-Z in plain js.

If you like to research it more you can google mdn keydown event and look for the Morzilla developer network.

Restrict keyboard character input with jQuery, How can I immediately stop a user typing characters for an input field for which only numbers How can you prevent inaccurate x y positioning when dragging? Input Get user Input. Use with Field to access all functionalities

How to immediately stop a user typing characters for an input field , An HTML form with an input field that can contain only three letters (no numbers or special characters):. <form action="/action_page.php"> Country code: <input� The field will stop accepting additional text input once the maximum character length has been reached. Disabled. Disabling a text field or text area prevents users from interacting with it in the web browser. This also activates the widget's Disabled style effect, making it appear greyed-out. There are two ways to disable a widget: Check the Disabled checkbox at the bottom of the Interactions pane.

HTML pattern Attribute, How can I prevent typing of the whitespace character in input textbox by To prevent typing whitespace in input field or textarea by using� Text Field. Text fields let users enter and edit text. Text fields allow users to enter text into a UI. They typically appear in forms and dialogs. TextField. The TextField wrapper component is a complete form control including a label, input and help text.

Prevent typing whitespace / disable spacebar in input, JavaScript: Restrict keyboard character input. This JavaScript restricts the character input the user can enter into an input or textarea. This is useful in aiding the� How many characters the input field is limited to. 0 = infinite. characterValidation: The type of validation to perform on a character. contentType: Specifies the type of the input text content. customCaretColor: Should a custom caret color be used or should the textComponent.color be used. inputType: The type of input expected. See InputField

  • all of them are either for special chars or for digits. Can't you just modify the code, rather than just blindly copy and paste?
  • use archive.plugins.jquery.com/project/limitkeypress
  • new link for limitkeypress brianjaeger.com/process.php
  • Doesn't work with chars created with modifiers pressed, does it?
  • Modifiers like shift and alt keys. Having them pressed can result in different characters - depending on the key, but the event.keyCode will be the same.
  • You can shorten the code by using [97,98,...].indexOf(e.which) != -1
  • This is the wrong way to go at it. It does not prevent pasting bad characters in the field.
  • or even return "12345NOABC".indexOf( String.fromCharCode(e.which) ) >= 0; to reduce it to one line
  • This is the wrong way to go at it. It does not prevent pasting bad characters in the field.
  • How does this differ from Pragnesh Chauhan's answer?
  • A little improvement so backspace shift delete can be used: (!regexVehiclePlate.test(e.key) && e.key.length == 1)