Skip to main content | Turn off styling Default page style

Question & Answer index

JavaScript to limit form field input text to specific characters

The first field will only accept characters I have identified as appropriate for a person's name (alphabetical, space, hyphen, apostrophe, and period).

The second field will only accept letters.

The third field will only accept numbers.

The fourth field will only accept "name characters" and "numbers".

As you press down on each key on your keyboard, the keystroke is intercepted and only allows the value to be "accepted" if it matches the required criteria.

Name Characters:

Letters Only:

Numbers Only:

Letters, Numbers, Hyphens, and Spaces:

Here is what the form looks like - I use the "onkeypress" event to call "inputLimiter" with the name of the type of text I want to limit it to:

Here is the function. Notice that the names of the text-types are very easy to configure - just add a new allow == 'Letters' then type out each fo the characters you want to allow in the AllowableCharacters= section.