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 represents a valid e-mail adress. 18 * 19 * @extends M.Validator 20 */ 21 M.EmailValidator = M.Validator.extend( 22 /** @scope M.EmailValidator.prototype */ { 23 24 /** 25 * The type of this object. 26 * 27 * @type String 28 */ 29 type: 'M.EmailValidator', 30 31 /** 32 * @type {RegExp} The regular expression for a valid e-mail address 33 */ 34 pattern: /^((?:(?:(?:\w[\.\-\+]?)*)\w)+)\@((?:(?:(?:\w[\.\-\+]?){0,62})\w)+)\.(\w{2,6})$/, 35 36 /** 37 * Validation method. Executes e-mail regex pattern to string. 38 * 39 * @param 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.test(obj.value)) { 48 return YES; 49 } 50 51 var err = M.Error.extend({ 52 msg: this.msg ? this.msg : obj.value + ' is not a valid email adress.', 53 code: M.ERR_VALIDATION_EMAIL, 54 errObj: { 55 msg: obj.value + ' is not a valid email adress.', 56 modelId: obj.modelId, 57 property: obj.property, 58 viewId: obj.viewId, 59 validator: 'EMAIL', 60 onSuccess: obj.onSuccess, 61 onError: obj.onError 62 } 63 }); 64 this.validationErrors.push(err); 65 66 return NO; 67 } 68 69 });