- This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
Viewing 9 posts - 1 through 9 (of 9 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Restricted the input field
hi,
i need to restrict my input field so it can only be filled by number, i manage to restricted the input field [Example](http://codepen.io/zhepyr/pen/Apnxd “Code pen”)
but it didn’t work like i want, all i want is to prevent user to type/input alphabet into the field.
ohh btw, if it’s possible i want to use only javascript not jquery
Hi,
HTML5 can do this: http://www.w3schools.com/html/html5_form_input_types.asp
And then validating the field after a submit and respond accordingly for browsers that don’t support this (yet).
Just like you would do to check if a valid email address was entered.
HTH
yeah i tried that before but i doesn’t compatible with some browser, and about validate the field when submit i already do that
but i think it’s more easier for user if we already restricted the input field just for number only.
Ok. Then jQuery:
$(‘.numbers’).keypress(function(key) {
if(key.charCode < 48 || key.charCode > 57) return false;
});
Formatting on CSS-tricks is a bit tricky sorry:
HTML:
jQuery:
$(‘.numbers’).keypress(function(key) {
if(key.charCode < 48 || key.charCode > 57) return false;
});
Sorry again.
Look at this: http://codepen.io/begetolo/pen/jvldz
You’re welcome. Glad I could help.