I have been searching through many HTML-related questions here and I am currently using the input tag pattern attribute to validate my inputs. This works by only allowing one or two numbers being input, and does not submit invalid data. However, I would prefer unwanted characters (decimal point and letters) to not be able to be put in the text box at all.
Asked
Active
Viewed 149 times
0
-
3Possible duplicate of [JQuery Input Mask Decimal point](http://stackoverflow.com/questions/25526436/jquery-input-mask-decimal-point) – ecorvo Jan 07 '16 at 22:00
-
@ecorvo I can see how that would work, and I may end up using that. However, I was told that there was an HTML5 solution by someone else who no longer has whatever he had working. I'm trying to find that solution before I try jQuery. – ladyskynet Jan 07 '16 at 22:03
-
you can define a number input field like this , it will filter out non numeric characters but will still allow you to put decimals.. – ecorvo Jan 07 '16 at 22:05
-
@ecorvo that still allows decimals to be input, just not submitted - the same as the pattern attribute does with my current solution. – ladyskynet Jan 07 '16 at 22:07
-
the only way to do this that I can think of is with regex, but for that I would just use a plugin, because the default HTML number input will let you put decimal since there is not an input type for integers only – ecorvo Jan 07 '16 at 22:14
-
You could use javascript and regular expressions – Andie2302 Jan 08 '16 at 00:04
1 Answers
0
This will do it for you https://jsfiddle.net/DIRTY_SMITH/cfckvvzw/4/
updated fiddle to allow numbers in numlock to work too https://jsfiddle.net/DIRTY_SMITH/cfckvvzw/5/
<input ID="FirstName1" class="test" onkeydown="return Fname(event);">
<script>
function Fname(evt) {
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
//checks for invalid keystrokes (numbers only)
if (charCode > 57 || charCode < 48) {
return false;
} else {
return true;
}
}
</script>

Adam Buchanan Smith
- 9,422
- 5
- 19
- 39
-
It is so hilarious to see you answer a question like this after the hard time you gave me for doing the same thing way back when. – Jan 08 '16 at 03:41
-
WONDERFUL. Thank you! Just on a side note - obviously this isn't necessary, but is there any way to limit the number of characters to one or two in this function? – ladyskynet Jan 08 '16 at 14:47
-
-
@TinyGiant and now you are complaining because I am being "more" helpful?! ....OK.... – Adam Buchanan Smith Jan 08 '16 at 17:39
-