knockout.js - Knockout Js Regex no negative numbers -


i'm trying find regex code disable negative numbers user input. i'm playing around code abit trying find right 1 haven't had success.

my current code is:

price: ko.observable().extend({         required: true,         pattern: '^[0-9].$'     }) 

in such case, why need allow user enter minus numbers in input field , validate input against negative number?

instead can prevent user entering negative numbers/strings.

this uses javascript, don't have write own validation routine. instead check validity.valid property. true if , if input falls within range.

solution 1:

<html>  <body>  <form action="#">    <input type="number" name="test" min=0 oninput="validity.valid||(value='');">  </form>  </body>  </html>

solution 2:

the below solution supports validate multiple inputs.

// select input element.  var numinput = document.queryselector('input');    // listen input event on numinput.  numinput.addeventlistener('input', function(){      // let's match digits.      var num = this.value.match(/^\d+$/);      if (num === null) {          // if have no match, value empty.          this.value = "";      }  }, false)
<input type="number" min="0" />

solution 3:

i haven't tested below solution, might well...

either '/^\d+$/' or '^\d+$' pattern may along current approach.

price: ko.observable().extend({         required: true,         pattern: '/^\d+$/'     }) 

original solution , reference here..

hope helps...


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -