1 /** 2 * @author Gillis Haasnoot <gillis.haasnoot@gmail.com> 3 * @package Banana.Controls 4 * @summary Regex validator 5 */ 6 7 goog.provide('Banana.Controls.Decorators.RegExValidator'); 8 9 goog.require('Banana.Controls.Decorators.Validator'); 10 11 /** @namespace Banana.Controls.Decorators.RegExValidator */ 12 namespace('Banana.Controls.Decorators').RegExValidator = Banana.Controls.Decorators.Validator.extend( 13 /** @lends Banana.Controls.Decorators.RegExValidator.prototype */ 14 { 15 16 /** 17 * Creates a regex validator 18 * use setRegExString to supply regex 19 * 20 * example 21 * new Banana.Controls.Decorators.RegExValidator( 22 new Banana.Controls.TextBox() 23 .dataSetBind('data','user.postcode') 24 ).setRegExString(/^[1-9]{1}[0-9]{3}\s?[A-Za-z]{2}$/) 25 .setValidateOnEventType('keyup') 26 .setInfoText('valide postcode needed example 1111AA') 27 * 28 * @param {Banana.Controls.InputControl} c 29 * @constructs 30 * @extends Banana.Controls.Decorators.Validator 31 */ 32 init : function(c) 33 { 34 this._super(c); 35 }, 36 37 /** 38 * @override 39 * 40 * @param {mixed} data 41 */ 42 validateData : function(data) 43 { 44 var exp = new RegExp(this.regExString); 45 46 if (data && data.match(exp)) 47 { 48 return true 49 } 50 51 return false; 52 }, 53 54 /** 55 * sets regex string 56 * 57 * @param {String} string 58 */ 59 setRegExString : function(string) 60 { 61 this.regExString = string; 62 return this; 63 } 64 });