Search code examples
dojodijit.form

Need to restrict a character "." to be entered in validation text box in dojo


I need to restrict a character from the validation text box in dojo; i.e. when the user enter's an "."(dot) into validation text box of dojo, The dot should not appear in the field.


Solution

  • Try this :

    In your html :

    <div id="tb"></div>
    

    In your JS :

    require(["dijit/form/ValidationTextBox",
         "dojo/on",
         "dojo/_base/event",
        "dojo/domReady!"], 
    function(ValidationTextBox, on, event){
        var textBox = new ValidationTextBox({}, "tb");
        on(textBox.domNode, "keypress", function(evt){
            var charOrCode = evt.charCode || evt.keyCode;
            if ("." === String.fromCharCode(charOrCode)) {
                event.stop(evt);
            }
        });
    });
    

    Declarative example :

    <div data-dojo-type="dijit/form/ValidationTextBox">
        <script type="dojo/on" data-dojo-event="keypress" data-dojo-args="evt">
            require(["dojo/_base/event"], function(event){
                var charOrCode = evt.charCode || evt.keyCode;
                if ("." === String.fromCharCode(charOrCode)) {
                    event.stop(evt);
                }
            });
        </script>
    </div>