PDA

View Full Version : Script Library Functions for Date and Number validation based on different Locales



ajay555
13 Oct 2009, 1:24 AM
In HTML form controls the numbers and dates are inputted based on the user's Locale setting. Does ExtJS has built-in library (function) which can be used to validate numbers based on the Locale setting. Similarly function to validate the date based on the locale.
The validate will be similar to something like checking for a number to be in between a specific range (this needs to handle the different decimal separator e.g. In Spanish the decimal separator is , instead of .).

Animal
13 Oct 2009, 3:13 AM
I agree more I18N effort is needed in Ext.util.Format. eg



Ext.apply(Ext.util.Format, {
dateFormat : "m/d/Y",

currencySymbol: '$',

/**
* Parse a value into a formatted date using the specified format pattern.
* @param {String/Date} value The value to format (Strings must conform to the format expected by the javascript Date object's <a href="http://www.w3schools.com/jsref/jsref_parse.asp">parse()</a> method)
* @param {String} format (optional) Any valid date format string (defaults to 'm/d/Y')
* @return {String} The formatted date string
*/
date : function(v, format){
if(!v){
return "";
}
if(!Ext.isDate(v)){
v = new Date(Date.parse(v));
}
return v.dateFormat(format || Ext.util.Format.dateFormat);
},

/**
* Format a number as local currency if the correct locale file is included.
* @param {Number/String} value The numeric value to format
* @return {String} The formatted currency string
*/
money : function(v){
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.');
var whole = ps[0];
var sub = ps[1] ? '.'+ ps[1] : '.00';
var r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ',' + '$2');
}
v = whole + sub;
if(v.charAt(0) == '-'){
return '-' + Ext.util.Format.currencySymbol + v.substr(1);
}
return Ext.util.Format.currencySymbol + v;
},
});


That way, the locale files could override default date and money formatting.