GibboK
GibboK

Reputation: 73988

NumberTextBox allow only integer

I need to make validation pass only for integer value in a NumberTextBox.

Using the following code, entering a decimal value in the NumberTextBox 54.454 wrongly validates.

I would like to know:

https://jsfiddle.net/9Lh3p0fb/7/

require(["dijit/form/NumberTextBox", "dojo/domReady!"], function(NumberTextBox){
    new NumberTextBox({
          name: "programmatic",
        constraints: {pattern: '@@@'}
    }, "programmatic").startup();
});

Upvotes: 2

Views: 1388

Answers (2)

user1162084
user1162084

Reputation: 462

I got a code from here and modified . please check if it solves your problem.

<input type='text' onkeypress='validate(event)' />

<script type="text/javascript">
    function validate(evt) {
      var theEvent = evt || window.event;
      var key = theEvent.keyCode || theEvent.which;
      key = String.fromCharCode( key );
      var regex = /[0-9]/;
      if( !regex.test(key) ) {
        theEvent.returnValue = false;
        if(theEvent.preventDefault) theEvent.preventDefault();
      }
    }
</script>` 

Upvotes: 0

epoch
epoch

Reputation: 16615

You can use places: 0 as a configuration option:

require(["dijit/form/NumberTextBox", "dojo/domReady!"], function (NumberTextBox) {
    new NumberTextBox({
        name: "programmatic",
        constraints: {
            pattern: '@@@',
            places: 0
        }
    }, "programmatic").startup();
});

Upvotes: 1

Related Questions