1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved. 4 // (c) 2011 panacoda GmbH. All rights reserved. 5 // Creator: Sebastian 6 // Date: 22.11.2010 7 // License: Dual licensed under the MIT or GPL Version 2 licenses. 8 // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE 9 // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE 10 // ========================================================================== 11 12 m_require('core/datastore/validator.js') 13 14 /** 15 * @class 16 * 17 * Validates if passed value is a number. Works with Strings and Numbers. Strings are parsed into numbers and then checked. 18 * 19 * @extends M.Validator 20 */ 21 M.NumberValidator = M.Validator.extend( 22 /** @scope M.NumberValidator.prototype */ { 23 24 /** 25 * The type of this object. 26 * 27 * @type String 28 */ 29 type: 'M.NumberValidator', 30 31 /** 32 * Validation method. If value's type is not "number" but a string, the value is parsed into an integer or float and checked versus the string value with '=='. 33 * The '==' operator makes an implicit conversion of the value. '===' would return false. 34 * 35 * @param {Object} obj Parameter object. Contains the value to be validated, the {@link M.ModelAttribute} object of the property and the model record's id. 36 * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false). 37 */ 38 validate: function(obj) { 39 if(typeof(obj.value) === 'number') { 40 return YES; 41 } 42 43 /* == makes implicit conversion */ 44 if(typeof(obj.value) === 'string' && (parseInt(obj.value) == obj.value || parseFloat(obj.value) == obj.value)) { 45 return YES; 46 } 47 48 var err = M.Error.extend({ 49 msg: this.msg ? this.msg : obj.value + ' is not a number.', 50 code: M.ERR_VALIDATION_NUMBER, 51 errObj: { 52 msg: obj.value + ' is not a number.', 53 modelId: obj.modelId, 54 property: obj.property, 55 viewId: obj.viewId, 56 validator: 'NUMBER', 57 onSuccess: obj.onSuccess, 58 onError: obj.onError 59 } 60 }); 61 62 this.validationErrors.push(err); 63 64 return NO; 65 } 66 }); 67