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