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 if value represents a valid URL. 17 * 18 * @extends M.Validator 19 */ 20 M.UrlValidator = M.Validator.extend( 21 /** @scope M.UrlValidator.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.UrlValidator', 29 30 /** 31 * @type {RegExp} The regular expression for a valid web URL 32 */ 33 pattern: /^(http[s]\:\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/, 34 35 /** 36 * Validation method. Executes urk regex pattern to string. 37 * 38 * @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. 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 url.', 51 modelId: obj.modelId, 52 property: obj.property, 53 viewId: obj.viewId, 54 validator: 'PHONE', 55 onSuccess: obj.onSuccess, 56 onError: obj.onError 57 }); 58 return NO; 59 } 60 61 });