Hello, I am attempting to create a regular expression for an input field that will only accept 4 numbers less than or equal to 2934, but will also allow for a negative number like -2394.
My goal is to permit the minus sign only at the beginning of the string, followed by exactly 4 numbers, not exceeding 2934 (both positive and negative, as they represent coordinates).
I have already tried the solutions provided here, but it seems like there is something missing.
function numonly(myfield, e, dec){
// maximum input value of 4196 x 4196
$("#mlat,#mlon").keyup(function() {
var val = $(this).val().replace(/[^0-9]+/,"");
if (val >= 2934){
!/^\s*$/.test(val);
val = (parseInt(val) > 2934) ? 2934 : val;
}
else {
(!/^\s*$/.test(val));
val = (parseInt(val) > 2934) ? 2934 : val;
}
$(this).val(val);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="mlat" type="text" name="mlat" maxlength="4" onkeypress="return numonly(this,event)"><br>
<input type="text" name="mlon" id="mlon" maxlength="4" onkeypress="return numonly(this,event)">