Replace Dojo NumberTextBox separator comma with a hyphen
How do I replace the comma formatting in a Dojo NumberTextBox with hyphen?
For example: Convert 123,456,789 into 123-456-78开发者_开发知识库9.It's not really a number anymore as much as a string pattern you want to preserve that happens to have numbers in it (I suppose you could say you want the thousands separator to be a dash instead of a comma? dojo.number.format supports overriding the thousands separator, but I don't think NumberTextBox does)
I owe you an example, but doughays says the way to do this would be to create a subclass of ValidationTextBox which provides a regexp matching the pattern you desire for the display/input and overrides parse and format methods to transform the number to/from that pattern.
I'm doing something similar for phone number inputs. This reformats a ValidationTextBox value as ###-###-####. (I elected not to use dojo/number because I already had an Array of digits and adding the dashes with splice is fairly straightforward)
The default filter method comes from _TextBoxMixin.js, which is where trim, uppercase, lowercase, and propercase is implemented.
declare('calpoly/form/FormattingPhoneNumber', [ValidationTextBox], {
regExp:"\\d{3}-\\d{3}-\\d{4}",
invalidMessage:"Please enter a 10-digit phone number",
filter:function(val){
val = lang.trim(val);
var s = [];
for(var i=0; i<val.length;i++){
if(!isNaN(val[i])){
s.push(val[i]);
}
}
if(s.length!=10){
// Not a 10-digit phone number, return what the user typed, regex will invalidate.
return val;
}
s.splice(3,0,'-');
s.splice(7,0,'-');
return s.join('');
}
});
精彩评论