I've form validation using JavaScript, I'm using this to allow only numbers on keyboard in input fields including numbers in top and num lock numbers. But this JavaScript only work for first input field, but I have several inputs fields how to adjust it?
The Problem is related to num lock numbers in keyboard right side.
In input second and third, num lock numbers not working. How to solve it?
Here is my code:
$(document).ready(function() {
$("#inp1").keypress(function(e) {
if (e.which != 8 && e.which != 0 && String.fromCharCode(e.which) != '-' && (e.which < 48 || e.which > 57)) {
$("#errmsg1").html("* Input digits (0 - 9)").show().delay(2000).fadeOut("slow");
return false;
}
});
$("#inp2").keydown(function(e) {
if (e.which != 8 && e.which != 0 && String.fromCharCode(e.which) != '-' && (e.which < 48 || e.which > 57)) {
$("#errmsg2").html("* Input digits (0 - 9)").show().delay(2000).fadeOut("slow");
return false;
}
});
$("#inp3").keydown(function(e) {
if (e.which != 8 && e.which != 0 && String.fromCharCode(e.which) != '-' && (e.which < 48 || e.which > 57)) {
$("#errmsg3").html("* Input digits (0 - 9)").show().delay(2000).fadeOut("slow");
return false;
}
});
});
#errmsg {
color: red;
}
#errmsg2 {
color: blue;
}
#errmsg3 {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Number : <input type="text" name="quantity" id="inp1" /> <span id="errmsg1"></span></p>
Number : <input type="text" name="quantity" id="inp2" /> <span id="errmsg2"></span></p>
Number : <input type="text" name="quantity" id="inp3" /> <span id="errmsg3"></span></p>