How to allow only numbers to be written in this textbox ?
<input type="text" class="textfield" value="" id="extra7" name="extra7">
How to allow only numbers to be written in this textbox ?
<input type="text" class="textfield" value="" id="extra7" name="extra7">
You could subscribe for the onkeypress event:
<input type="text" class="textfield" value="" id="extra7" name="extra7" onkeypress="return isNumber(event)" />
and then define the isNumber
function:
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
You can see it in action here.
if
clause. If it's larger than 57, it's always larger than 31. I think you can eliminate the parentheses. –
Unpolled if ((charCode >= 48 && charCode <= 57) || ((charCode >= 96 && charCode <= 105))) { return true; } return false;
–
Cogwheel With HTML5 you can do
<input type="number">
You can also use a regex pattern to limit the input text.
<input type="text" pattern="^[0-9]*$" />
You also can use some HTML5 attributes, some browsers might already take advantage of them (type="number" min="0"
).
Whatever you do, remember to re-check your inputs on the server side: you can never assume the client-side validation has been performed.
© 2022 - 2024 — McMap. All rights reserved.
12.3
,-4
,V
,six
? – Manuelmanuela