Only allow letters in a text field | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Only allow letters in a text field

I'd like to know if it's possible to only allow a letter input in a text field in HTML (the opposite to <input type="number">). If it isn't, what would be the best way to detect when an user inputs a non-letter character (spaces and special characters are also forbidden), so I can alert a message?

31st Aug 2018, 4:06 PM
Juca Navaz Reque
Juca Navaz Reque - avatar
4 Answers
+ 3
Just set input tag to <input type="text" pattern="[A-Za-z]*" title="Letters only"/> https://code.sololearn.com/W5hRLPzw65Io/?ref=app
31st Aug 2018, 4:37 PM
Calviղ
Calviղ - avatar
+ 1
Just use the 'text' type. <input type="text"> https://www.w3schools.com/tags/att_input_type.asp button Defines a clickable button checkbox Defines a checkbox color Defines a color picker date Defines a date control (year, month, day (no time)) datetime-local Defines a date and time control email Defines a field for an e-mail address file Defines a file-select field and a "Browse" button (for file uploads) hidden Defines a hidden input field image Defines an image as the submit button month Defines a month and year control (no timezone) number Defines a field for entering a number password Defines a password field radio Defines a radio button range Defines a range control (like a slider control) reset Defines a reset button search Defines a text field for entering a search string submit Defines a submit button tel Defines a field for entering a telephone number text Default. Defines a single-line text field time Defines a control for entering a time (no timezone) url Defines a field for entering a URL week Defines a week and year control (no timezone)
31st Aug 2018, 4:14 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
I just read your question again. You're trying to focus on a SINGLE letter, and no special chars, right? If so, you'll want to use Javascript/jQuery to handle that from the client side. That's probably most efficient means.
31st Aug 2018, 4:16 PM
Fata1 Err0r
Fata1 Err0r - avatar
0
Fata1 Err0r I can't use type="text", as it allows every kind of character as input. What I need is an input with any amount of letters, not numbers, not spaces, not special characters... Only letters. I supposed I had to use some JS, but I don't know how. Can you explain?
31st Aug 2018, 4:36 PM
Juca Navaz Reque
Juca Navaz Reque - avatar