2

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:

  • How to validate only a integer values?
  • Is it possible to avoid the user entering the .?

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

require(["dijit/form/NumberTextBox", "dojo/domReady!"], function(NumberTextBox){
    new NumberTextBox({
          name: "programmatic",
        constraints: {pattern: '@@@'}
    }, "programmatic").startup();
});
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • http://stackoverflow.com/questions/30323610/check-if-a-textbox-contains-numbers-only/30323668#30323668 might help you – Tushar Jul 03 '15 at 06:29
  • useful link regarding pattern property https://dojotoolkit.org/reference-guide/1.6/quickstart/numbersDates.html – GibboK Jul 03 '15 at 06:55

2 Answers2

1

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();
});
epoch
  • 16,396
  • 4
  • 43
  • 71
  • Just a quick question...Do you know if it is possible disabling entering the ./, on the control itself? Using NumberTextBox. I am aware is possible with some work around on event keydown, but I am not sure if it is supported my dijit itself. Thanks for your time on this. – GibboK Jul 03 '15 at 06:38
  • @GibboK, no problem, not too sure about that, ill have a look quickly – epoch Jul 03 '15 at 07:42
0

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>` 
Community
  • 1
  • 1
user1162084
  • 462
  • 1
  • 6
  • 18