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 represents a valid e-mail adress. 17 * 18 * @extends M.Validator 19 */ 20 M.EmailValidator = M.Validator.extend( 21 /** @scope M.EmailValidator.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.EmailValidator', 29 30 /** 31 * @type {RegExp} The regular expression for a valid e-mail address 32 */ 33 pattern: /^((?:(?:(?:\w[\.\-\+]?)*)\w)+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/, 34 35 /** 36 * Validation method. Executes e-mail regex pattern to string. 37 * 38 * @param obj Parameter object. Contains the value to be validated, the {@link M.ModelAttribute} object of the property and the model record's id. 39 * @returns {Boolean} Indicating whether validation passed (YES|true) or not (NO|false). 40 */ 41 validate: function(obj) { 42 if (typeof(obj.value !== 'string')) { 43 return NO; 44 } 45 46 if (this.pattern.exec(obj.value)) { 47 return YES; 48 } 49 this.validationErrors.push({ 50 msg: obj.value + ' is not a valid email adress.', 51 modelId: obj.modelId, 52 property: obj.property, 53 viewId: obj.viewId, 54 validator: 'EMAIL', 55 onSuccess: obj.onSuccess, 56 onError: obj.onError 57 }); 58 return NO; 59 } 60 61 });