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 a string if it matches a phone number pattern.
 18  *
 19  * @extends M.Validator
 20  */
 21 M.PhoneValidator = M.Validator.extend(
 22 /** @scope M.PhoneValidator.prototype */ {
 23 
 24     /**
 25      * The type of this object.
 26      *
 27      * @type String
 28      */
 29     type: 'M.PhoneValidator',
 30 
 31     /**
 32      * It is assumed that phone numbers consist only of: 0-9, -, /, (), .
 33      * @type {RegExp} The regular expression detecting a phone adress.
 34      */
 35     pattern: /^[0-9-\/()+\.\s]+$/,
 36 
 37     /**
 38      * Validation method. Executes e-mail regex pattern to string. 
 39      *
 40      * @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.
 41      * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false).
 42      */
 43     validate: function(obj) {
 44         if (typeof(obj.value !== 'string')) {
 45             return NO;
 46         }
 47 
 48         if (this.pattern.exec(obj.value)) {
 49             return YES;
 50         }
 51 
 52 
 53         var err = M.Error.extend({
 54             msg: this.msg ? this.msg : obj.value + ' is not a phone number.',
 55             code: M.ERR_VALIDATION_PHONE,
 56             errObj: {
 57                 msg: obj.value + ' is not a phone number.',
 58                 modelId: obj.modelId,
 59                 property: obj.property,
 60                 viewId: obj.viewId,
 61                 validator: 'PHONE',
 62                 onSuccess: obj.onSuccess,
 63                 onError: obj.onError
 64             }
 65         });
 66 
 67         this.validationErrors.push(err);
 68         return NO;
 69     }
 70     
 71 });