I have a dijit validation textbox i am attempting to automatically insert a comma after each word has been typed.However i am not successful under is my code and a fiddle for reference.

Jsp

<body class="claro">

    <input
        data-dojo-type="dijit.form.ValidationTextBox"
        data-dojo-props="uppercase:true"
        id="validationTextBox"/>
</body>  

Javascript

dojo.require("dijit.form.ValidationTextBox");

dojo.ready(function() {   

     dojo.connect(dijit.byId("validationTextBox"), "onkeyup", function() {

         //alert('hello');
  var str = dijit.byId("validationTextBox").value.replace(/(\w)[\s,]+(\w?)/g, '$1, $2');
  if (str!= dijit.byId("validationTextBox").value) dijit.byId("validationTextBox").value = str; 
    });

});
有帮助吗?

解决方案

The idea was right and the regular expression seemed to work but I didn't look at it too carefully.

You seemed to be coding using an older version of Dojo (pre 1.6) yet the fiddle was set to use Dojo 1.9.3 which has AMD support.

Here's a working example using AMD. There are many ways that you can accomplish this. I went for the Stateful method. You can 'watch' dijit properties and call function when they change. I also set the intermediateChanges to true for possibly a nicer effect.

http://jsfiddle.net/RichAyotte/DqVt7/

<body class="claro">
    <input
    data-dojo-type="dijit/form/ValidationTextBox"
        data-dojo-props="uppercase:true, intermediateChanges:true"
        id="validationTextBox"
    />
</body>

require([
    'dijit/form/ValidationTextBox'
    , 'dijit/registry'
    , 'dojo/domReady!'
], function(
    ValidationTextBox
    , registry
){
    var tb = registry.byId('validationTextBox');
    tb.watch('value', function(property, oldValue, newValue) {
      tb.set('value', newValue.replace(/(\w)[\s,]+(\w?)/g, '$1, $2')); 
    });
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top