views:

884

answers:

3

Hello everybody,

I was today seeking the net for information about textbox-validation, but even on Dojo-Homepage I couldn't get any useful information.

My problem: I've got a NumberSpinner, in which only numbers in steps of ten (10, 20, 30, ...) should be allowed. But I've got no idea how to set a validator for this. In the 'constraints'-statement there seems to be no possibility to do this. And I don't know how to use the validator-function so that the box shows me warning sign immediately when typing in somethin wrong.

Another question is how to check if any part of a form is not valid before sending it. Is there an attribute in every input/select-box like 'valid' to check them all at once?

Oh, one hint, I create all widgets programmatically.

Hopefully anyone out there can help me!!!

Best regards, Robin

A: 

You can override the NumberSpinner's isValid() method. For example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
<head>
  <title>class</title>
  <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dijit/themes/soria/soria.css" />
  <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.3/dojo/dojo.xd.js"&gt;&lt;/script&gt;
  <script type="text/javascript">
    dojo.config.parseOnLoad = true;
    dojo.addOnLoad(function(){
      dojo.require('dijit.form.NumberSpinner');
      dojo.require('dojo.parser');
   dojo.addOnLoad(function(){
    new dijit.form.NumberSpinner({
     isValid: function(isFocused) {
      var value = parseFloat(this.attr('value'));
      if (isNaN(value) || (value % 10 != 0)) {
       return false;
      }
      else {
       return true;
      }
     }
    }, 'here');

   });

    });
  </script>
</head>
<body class="soria">
 <div id="here"></div>
</body>
</html>
Michael B
A: 

dijit.form.NumberSpinner is derived from dijit.form.ValidationTextBox, and as such it would accept the same arguments (see dijit.form.ValidationTextBox docs and inline docs in its source code). Just write a regular expression (as a string) that can validate your input. Something like that should do the trick:

var box = new dijit.form.NumberSpinner({
  regExpGen: function(){ return "\\d+0"; }
}, "my_node");
Eugene Lazutkin
A: 

You can also use the delta/smallDelta attribute for this. See the example at Dojo Campus

peller