Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  138] [ 3]  / answers: 1 / hits: 69128  / 12 Years ago, wed, november 28, 2012, 12:00:00

Is it possible to restrict the input of certain characters in HTML5/JavaScript? For example, could I have an input textbox on the screen and if the user tries to type a letter in it, it wouldn't show up in the box because I've restricted it to only numbers?


I know you can use a pattern which will be checked on submit, but I want the "bad" characters to just never be entered at all.


More From » html

 Answers
36

Use html5 pattern attribute for inputs:



<input type=text pattern=d* title=Only digits />


OR



Use html5 number type for input :



<input type=number />

[#81748] Tuesday, November 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dahlias

Total Points: 730
Total Questions: 104
Total Answers: 101

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;