0

I'm trying implements a regex on input to allow ONLY numbers and logical operators(>, <, >= and <=).

With this code I can allow only numbers. I tried to do some modifications to allow logical operatos too but without successful.

My Code:

$(document).ready(function(){
   
  $('#txt').on("keyup", function (event) {
    if (/\D/g.test(this.value)) {
      this.value = this.value.replace(/[^0-9]|^0+(?!$)/g, '');
    }
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt">

Somoeone can help me?

How I can modify my regex to allow only numbers and logical operators(>, <, >= and <=)?

Zkk
  • 691
  • 8
  • 22

1 Answers1

0

Try this regex change below:

$(document).ready(function() {

  $('#txt').on("keyup", function(event) {
    if (/[\D\>\<\=]/g.test(this.value)) {
      this.value = this.value.replace(/[^0-9\>\<\=]|^0+(?!$)/g, '');
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt">
Sinto
  • 3,710
  • 11
  • 32
  • 59