diegz25
diegz25

Reputation: 45

How to only allow alphanumeric on textbox using pure HTML

Suppose I have a textbox, and I want it to only accept alphanumeric characters (not case sensitive). I already did it by using javascript and regex.test() but I want the simplest implementation possible. Can this be done by using HTML only? For example, I know that we can limit a textbox to accept numbers by doiing <input type="number">. But I want the textbox to accept both text and number type and restrict special characters. Thank you.

Upvotes: 1

Views: 11029

Answers (2)

NoiK
NoiK

Reputation: 182

Take a look at this feature for input patterns.

You can do that:

<input type="text" id="onlyletters" name="onlyletters" pattern="[a-zA-Z0-9]+" title="Only letters">

Inside the pattern attribute, you can use any regular expression.

Upvotes: 2

limido
limido

Reputation: 327

If you are using the textarea element then the answer is no.

If, however, you are using the input element, then the answer is yes - See specifying a pattern, it can accept a Javascript regex, but it's done only with HTML.

Upvotes: 0

Related Questions