1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved. 4 // Creator: Sebastian 5 // Date: 22.11.2010 6 // License: Dual licensed under the MIT or GPL Version 2 licenses. 7 // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE 8 // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE 9 // ========================================================================== 10 11 m_require('core/datastore/validator.js') 12 13 /** 14 * @class 15 * 16 * Validates a string if it matches a phone number pattern. 17 * 18 * @extends M.Validator 19 */ 20 M.PhoneValidator = M.Validator.extend( 21 /** @scope M.PhoneValidator.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.PhoneValidator', 29 30 /** 31 * It is assumed that phone numbers consist only of: 0-9, -, /, (), . 32 * @type {RegExp} The regular expression detecting a phone adress. 33 */ 34 pattern: /^[0-9-\/()+\.\s]+$/, 35 36 /** 37 * Validation method. Executes e-mail regex pattern to string. 38 * 39 * @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. 40 * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false). 41 */ 42 validate: function(obj) { 43 if (typeof(obj.value !== 'string')) { 44 return NO; 45 } 46 47 if (this.pattern.exec(obj.value)) { 48 return YES; 49 } 50 this.validationErrors.push({ 51 msg: obj.value + ' is not a phone number.', 52 modelId: obj.modelId, 53 property: obj.property, 54 viewId: obj.viewId, 55 validator: 'PHONE', 56 onSuccess: obj.onSuccess, 57 onError: obj.onError 58 }); 59 return NO; 60 } 61 62 });