RogerHN
RogerHN

Reputation: 614

Javascript regex to check only numbers and minus at the start of string

Hello I am trying to make a regex in this input field to accept only 4 numbers not greater than 2934, but accept a negative one too (-2394).

I want to allow the minus sign only at the start of the string, and then 4 numbers only, not greater than 2934 (positive and negative, because they are coordinates.

Already tried the given solutions here, but I am missing something.

function numonly(myfield, e, dec){
      // max 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)">

Upvotes: 0

Views: 2202

Answers (3)

swetansh kumar
swetansh kumar

Reputation: 475

you can use the following method :

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
<input id="mlat" type="text" name="mlat" maxlength="5" onkeypress="return numonly(this,event)"><br>
<input type="text" name="mlon" id="mlon" maxlength="5"  onkeypress="return numonly(this,event)">
<script type="text/javascript">

function numonly(myfield, e, dec){

  // max input value of 4196 x 4196
  $("#mlat").keyup(function() {
    var val = $(this).val().replace(/-?\d+[^0-9]+/,"");
    if (val => 2934){
      !/^\s*$/.test(val);
      if (val > 0) {
        val = (parseInt(val) > 2934) ? 2934 : val;
      }else{
        val = (parseInt(val) > -2394) ? val : -2394;
      }

    }
    else {
      (!/^\s*$/.test(val));
      if (val > 0) {
        val = (parseInt(val) > 2934) ? 2934 : val;
      }else{
        val = (parseInt(val) > -2394) ? val : -2394;
      }
    }
    $(this).val(val);
  })
};


</script>
</body>
</html>

and replace the maxlength to 5 to handle - sign in input tag.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68443

I am trying to make a regex in this input field to accept only 4 numbers not greater than 2934, but accept a negative one too (-2394).

No need for regex here, use

var minValue = -2394;
var maxValue = 2934;    
var val = +$(this).val().replace(/[^0-9-]+/g,""); //using your own regex to replace non-numeric characters
var isValid = !isNaN( val ) && val < maxValue && val > minValue ;

Also, i noticed that you want to make these values as upper and lower bound

if ( !isNaN( val ) )
{
    var finalVal  = isValid ? val : (val < 0 ? minValue  : maxValue);
}

Upvotes: 1

Farhan Qasim
Farhan Qasim

Reputation: 990

function numonly(myfield, e, dec){
  // max input value of 4196 x 4196
  $("#mlat,#mlon").keyup(function() {
    var val = $(this).val().replace(/[^0-9-]+/,"");
    if (parseInt(val) > 2934){
      val = (parseInt(val) > 2934) ? 2934 : val;
    }
    else {
      val = (parseInt(val) < -2934) ? -2934 : val;
    }
    $(this).val(val);
  });
}

Increase your max length to 5 to cater the '-'

<input id="mlat" type="text" name="mlat" maxlength="5" onkeypress="return numonly(this,event)"><br>
<input type="text" name="mlon" id="mlon" maxlength="5"  onkeypress="return numonly(this,event)">

Upvotes: 0

Related Questions