PDA

View Full Version : Ext.ux.NumberFieldLeadingZeroes



tobiu
30 May 2008, 7:59 AM
hi together,

it's not a big deal, but i had cases, where i like to use numberfields with leading zeroes, for example telephone-numbers etc.

one way might be to use a regex on TextFields, but then other functionality would be lost.

to shorten the code, we could extend the original NumberField.

to allow leading zeroes, just set allowLeadingZeroes : true in the config.


feedback and ideas for improvement is welcome!


kind regards, tobiu




/*
* Ext JS Library 2.1
* Copyright(c) 2006-2008, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/

/**
* @class Ext.ux.NumberFieldLeadingZeroes
* @extends Ext.form.TextField
* Numeric text field that provides automatic keystroke filtering and numeric validation.
* @constructor
* Creates a new NumberFieldLeadingZeroes
* @param {Object} config Configuration options
*/
Ext.ux.NumberFieldLeadingZeroes = Ext.extend(Ext.form.TextField, {
/**
* @cfg {String} fieldClass The default CSS class for the field (defaults to "x-form-field x-form-num-field")
*/
fieldClass: "x-form-field x-form-num-field",
/**
* @cfg {Boolean} allowDecimals False to disallow decimal values (defaults to true)
*/
allowDecimals : true,
/**
* @cfg {String} decimalSeparator Character(s) to allow as the decimal separator (defaults to '.')
*/
decimalSeparator : ".",
/**
* @cfg {Number} decimalPrecision The maximum precision to display after the decimal separator (defaults to 2)
*/
decimalPrecision : 2,
/**
* @cfg {Boolean} allowNegative False to prevent entering a negative sign (defaults to true)
*/
allowNegative : true,
/**
* @cfg {Number} minValue The minimum allowed value (defaults to Number.NEGATIVE_INFINITY)
*/
minValue : Number.NEGATIVE_INFINITY,
/**
* @cfg {Number} maxValue The maximum allowed value (defaults to Number.MAX_VALUE)
*/
maxValue : Number.MAX_VALUE,
/**
* @cfg {String} minText Error text to display if the minimum value validation fails (defaults to "The minimum value for this field is {minValue}")
*/
minText : "The minimum value for this field is {0}",
/**
* @cfg {String} maxText Error text to display if the maximum value validation fails (defaults to "The maximum value for this field is {maxValue}")
*/
maxText : "The maximum value for this field is {0}",
/**
* @cfg {String} nanText Error text to display if the value is not a valid number. For example, this can happen
* if a valid character like '.' or '-' is left in the field with no number (defaults to "{value} is not a valid number")
*/
nanText : "{0} is not a valid number",
/**
* @cfg {String} baseChars The base set of characters to evaluate as valid numbers (defaults to '0123456789').
*/
baseChars : "0123456789",
/**
* @cfg {Boolean} allowLeadingZeroes False to prevent entering a leading zeroes (defaults to false).
*/
allowLeadingZeroes : false,

// private
initEvents : function(){
Ext.form.NumberField.superclass.initEvents.call(this);
var allowed = this.baseChars+'';
if(this.allowDecimals){
allowed += this.decimalSeparator;
}
if(this.allowNegative){
allowed += "-";
}
this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
var keyPress = function(e){
var k = e.getKey();
if(!Ext.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
return;
}
var c = e.getCharCode();
if(allowed.indexOf(String.fromCharCode(c)) === -1){
e.stopEvent();
}
};
this.el.on("keypress", keyPress, this);
},

// private
validateValue : function(value){
if(!Ext.form.NumberField.superclass.validateValue.call(this, value)){
return false;
}
if(value.length < 1){ // if it's blank and textfield didn't flag it then it's valid
return true;
}
value = String(value).replace(this.decimalSeparator, ".");
if(isNaN(value)){
this.markInvalid(String.format(this.nanText, value));
return false;
}
var num = this.parseValue(value);
if(num < this.minValue){
this.markInvalid(String.format(this.minText, this.minValue));
return false;
}
if(num > this.maxValue){
this.markInvalid(String.format(this.maxText, this.maxValue));
return false;
}
return true;
},

getValue : function(){
return this.fixPrecision(this.parseValue(Ext.form.NumberField.superclass.getValue.call(this)));
},

setValue : function(v){
v = typeof v == 'number' ? v : this.parseFloatZeroes(String(v).replace(this.decimalSeparator, "."));
v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
Ext.form.NumberField.superclass.setValue.call(this, v);
},

// private
parseValue : function(value){
value = this.parseFloatZeroes(String(value).replace(this.decimalSeparator, "."));
return isNaN(value) ? '' : value;
},

// private
parseFloatZeroes : function(value){
if(this.allowLeadingZeroes){
var zeroes = "";
while(value.substr(0, 1) == "0"){
zeroes += "0";
value = value.substring(1, value.length);
}
return zeroes + parseFloat(value);
} else {
return parseFloat(value);
}
},

// private
parseFloatZeroesToFix : function(value){
if(this.allowLeadingZeroes){
var zeroes = "";
while(value.substr(0, 1) == "0"){
zeroes += "0";
value = value.substring(1, value.length);
}
return zeroes + parseFloat(value).toFixed(this.decimalPrecision);
} else {
return parseFloat(value).toFixed(this.decimalPrecision);
}
},

// private
fixPrecision : function(value){
var nan = isNaN(value);
if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
return nan ? '' : value;
}
return this.parseFloatZeroes(this.parseFloatZeroesToFix(value));
},

beforeBlur : function(){
var v = this.parseValue(this.getRawValue());
if(v){
this.setValue(this.fixPrecision(v));
}
}
});
Ext.reg('numberFieldLeadingZeroes', Ext.ux.NumberFieldLeadingZeroes);

tobiu
30 May 2008, 8:12 AM
ok, here is the version extending a NumberField.

The code is shorter. Both are not tested much yet!



/**
* @class Ext.form.NumberField
* @extends Ext.form.TextField
* Numeric text field that provides automatic keystroke filtering and numeric validation.
* @constructor
* Creates a new NumberField
* @param {Object} config Configuration options
*/
Ext.ux.NumberFieldZeroes = Ext.extend(Ext.form.NumberField, {
/**
* @cfg {Boolean} allowLeadingZeroes False to prevent entering a leading zeroes (defaults to false).
*/
allowLeadingZeroes : false,

setValue : function(v){
v = typeof v == 'number' ? v : this.parseFloatZeroes(String(v).replace(this.decimalSeparator, "."));
v = isNaN(v) ? '' : String(v).replace(".", this.decimalSeparator);
Ext.form.NumberField.superclass.setValue.call(this, v);
},

// private
parseValue : function(value){
value = this.parseFloatZeroes(String(value).replace(this.decimalSeparator, "."));
return isNaN(value) ? '' : value;
},

// private
parseFloatZeroes : function(value){
if(this.allowLeadingZeroes){
var zeroes = "";
while(value.substr(0, 1) == "0"){
zeroes += "0";
value = value.substring(1, value.length);
}
return zeroes + parseFloat(value);
} else {
return parseFloat(value);
}
},

// private
parseFloatZeroesToFix : function(value){
if(this.allowLeadingZeroes){
var zeroes = "";
while(value.substr(0, 1) == "0"){
zeroes += "0";
value = value.substring(1, value.length);
}
return zeroes + parseFloat(value).toFixed(this.decimalPrecision);
} else {
return parseFloat(value).toFixed(this.decimalPrecision);
}
},

// private
fixPrecision : function(value){
var nan = isNaN(value);
if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
return nan ? '' : value;
}
return this.parseFloatZeroes(this.parseFloatZeroesToFix(value));
}
});
Ext.reg('numberfieldZeroes', Ext.ux.NumberFieldZeroes);