Reputation: 23
I have a textbox on my form where a user must fill in a month, i figured out how to input numeric values but I'd like my textbox to receive numeric values between 1 and 12. I've already tried the min, max but none of them worked.
HTML Code:
<input type="text" class="form-control" name="Month" id="Month"onkeypress="return isNumber(event)"maxlength="2"placeholder="MM"required="required">
JavaScript Code:
<script type="text/javascript">
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;
}
</script>
Upvotes: 2
Views: 781
Reputation: 2098
Here is the simple code to check month, if number beyond limit it will reset and ask to type again
function checkMonth(textbox){
if(!(textbox.value > 0 && textbox.value <= 12 )){
textbox.value = "";
textbox.focus();
}
}
<input type="number" id="month" min="1" max="12" onKeyUp="checkMonth(this)">
Upvotes: 0
Reputation: 16777
You should forego the extra JS and instead use <input type="number">
and the min
/max
attributes:
<input type="number" class="form-control" name="Month" id="Month" min="1" max="12" placeholder="MM" required>
Upvotes: 1