1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 sc_require('validators/validator') ;
  9 sc_require('system/utils/misc');
 10 
 11 /**
 12   Handles parsing and validating of numbers.
 13   
 14   @extends SC.Validator
 15   @author Charles Jolley
 16   @version 1.0
 17   @class
 18 */
 19 SC.Validator.Number = SC.Validator.extend(
 20 /** @scope SC.Validator.Number.prototype */ {
 21 
 22   /**
 23     Number of decimal places to show.  
 24     
 25     If 0, then numbers will be treated as integers.  Otherwise, numbers will
 26     show with a fixed number of decimals.
 27   */
 28   places: 0,
 29   
 30   fieldValueForObject: function(object, form, field) {
 31     switch(SC.typeOf(object)) {
 32       case SC.T_NUMBER:
 33         object = object.toFixed(this.get('places')) ;
 34         break ;
 35       case SC.T_NULL:
 36       case SC.T_UNDEFINED:
 37         object = '';
 38         break ;
 39     }
 40     return object ;
 41   },
 42 
 43   objectForFieldValue: function(value, form, field) {
 44     // strip out commas
 45     var result;
 46     value = value.replace(/,/g,'');
 47     switch(SC.typeOf(value)) {
 48       case SC.T_STRING:
 49         if (value.length === 0) {
 50           value = null ;
 51         } else if (this.get('places') > 0) {
 52           value = parseFloat(value) ;
 53         } else {
 54           if(value.length===1 && value.match(/-/)) value = null;
 55           else {
 56             result = parseInt(value,0) ;
 57             if(isNaN(result)){
 58               value = SC.uniJapaneseConvert(value);
 59               value = parseInt(value,0) ;
 60               if(isNaN(value)) value='';
 61             }else value = result;
 62           }
 63         }
 64         break ;
 65       case SC.T_NULL:
 66       case SC.T_UNDEFINED:
 67         value = null ;
 68         break ;
 69     }
 70     return value ;
 71   },
 72   
 73   validate: function(form, field) { 
 74     var value = field.get('fieldValue') ;
 75     return (value === '') || !(isNaN(value) || isNaN(parseFloat(value))) ; 
 76   },
 77   
 78   validateError: function(form, field) {
 79     var label = field.get('errorLabel') || 'Field' ;
 80     return SC.$error(SC.String.loc("Invalid.Number(%@)", label), label) ;
 81   },
 82   
 83   /** 
 84     Allow only numbers, dashes, period, and commas
 85   */
 86   validateKeyDown: function(form, field, charStr) {
 87     if(!charStr) charStr = "";
 88     var text = field.$input().val();
 89     if (!text) text='';
 90     text+=charStr;
 91 
 92     if(this.get('places')===0){
 93       if(charStr.length===0) return true;
 94       else return text.match(/^[\-{0,1}]?[0-9,\0]*/)[0]===text;
 95     }else {
 96       if(charStr.length===0) return true;
 97       else return text.match(/^[\-{0,1}]?[0-9,\0]*\.?[0-9\0]+/)===text;
 98     }
 99   }
100     
101 }) ;
102