diff --git a/etemplate/inc/class.etemplate_widget_textbox.inc.php b/etemplate/inc/class.etemplate_widget_textbox.inc.php index 5ff4ff651e..d3912d89b2 100644 --- a/etemplate/inc/class.etemplate_widget_textbox.inc.php +++ b/etemplate/inc/class.etemplate_widget_textbox.inc.php @@ -97,7 +97,7 @@ class etemplate_widget_textbox extends etemplate_widget * - needed: value must NOT be empty * - min, max: int and float widget only * - maxlength: maximum length of string (longer strings get truncated to allowed size) - * - preg: perl regular expression incl. delimiters (set by default for int, float and colorpicker) + * - validator: perl regular expression incl. delimiters (set by default for int, float and colorpicker) * - int and float get casted to their type * * @param string $cname current namespace @@ -112,18 +112,19 @@ class etemplate_widget_textbox extends etemplate_widget if (!$this->is_readonly($cname, $form_name)) { - if (!isset($this->attrs['preg'])) + if (!isset($this->attrs['validator'])) { switch($this->type) { + case 'int': case 'integer': - $this->attrs['preg'] = '/^-?[0-9]*$/'; + $this->attrs['validator'] = '/^-?[0-9]*$/'; break; case 'float': - $this->attrs['preg'] = '/^-?[0-9]*[,.]?[0-9]*$/'; + $this->attrs['validator'] = '/^-?[0-9]*[,.]?[0-9]*$/'; break; case 'colorpicker': - $this->attrs['preg'] = '/^(#[0-9a-f]{6}|)$/i'; + $this->attrs['validator'] = '/^(#[0-9a-f]{6}|)$/i'; break; } } @@ -150,7 +151,7 @@ class etemplate_widget_textbox extends etemplate_widget { $value = mb_substr($value,0,(int) $this->attrs['maxlength']); } - if ($this->attrs['preg'] && !preg_match($this->attrs['preg'],$value)) + if ($this->attrs['validator'] && !preg_match($this->attrs['validator'],$value)) { switch($this->type) { @@ -161,7 +162,7 @@ class etemplate_widget_textbox extends etemplate_widget self::set_validation_error($form_name,lang("'%1' is not a valid floatingpoint number !!!",$value),''); break; default: - self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[preg]', '$value')"*/,''); + self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[validator]', '$value')"*/,''); break; } } diff --git a/etemplate/js/et2_core_phpExpressionCompiler.js b/etemplate/js/et2_core_phpExpressionCompiler.js index 44532fa656..63dbabe2ba 100644 --- a/etemplate/js/et2_core_phpExpressionCompiler.js +++ b/etemplate/js/et2_core_phpExpressionCompiler.js @@ -70,6 +70,12 @@ str += '$'; break; } + // check for regular expression "/ $/" + if (_p.expr.charAt(_p.pos) == _p.expr.charAt(0)) + { + str += '$'; + break; + } if (str) { _tree.push(str); str = ""; diff --git a/etemplate/js/et2_widget_textbox.js b/etemplate/js/et2_widget_textbox.js index 24a059e172..9559fd01db 100644 --- a/etemplate/js/et2_widget_textbox.js +++ b/etemplate/js/et2_widget_textbox.js @@ -62,10 +62,16 @@ var et2_textbox = et2_inputWidget.extend( "type": "integer", "default": -1, "description": "Multiline field width - better to use CSS" + }, + "validator": { + "name": "Validator", + "type": "string", + "default": et2_no_init, + "description": "Perl regular expression eg. '/^[0-9][a-f]{4}$/i'" } }, - legacyOptions: ["size", "maxlength"], + legacyOptions: ["size", "maxlength", "validator"], /** * Constructor @@ -126,6 +132,37 @@ var et2_textbox = et2_inputWidget.extend( return this._super.apply(this, arguments); }, + /** + * Clientside validation using regular expression in "validator" attribute + * + * @param {array} _messages + */ + isValid: function(_messages) + { + var ok = true; + // Check input is valid + if(this.options && this.options.validator && !this.options.readonly) + { + if (typeof this.options.validator == 'string') + { + var parts = this.options.validator.split('/'); + var flags = parts.pop(); + if (parts.length < 2 || parts[0] !== '') + { + _messages.push(this.egw().lang("'%1' has an invalid format !!!", this.options.validator)); + return false; // show invalid expression + } + parts.shift(); + this.options.validator = new RegExp(parts.join('/'), flags); + } + var value = this.getValue(); + if (!(ok = this.options.validator.test(value))) + { + _messages.push(this.egw().lang("'%1' has an invalid format !!!", value)); + } + } + return this._super.apply(this, arguments) && ok; + }, /** * Set input widget size