4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.form.TextField
15 * @extends Roo.form.Field
16 * Basic text field. Can be used as a direct replacement for traditional text inputs, or as the base
17 * class for more sophisticated input controls (like {@link Roo.form.TextArea} and {@link Roo.form.ComboBox}).
19 * Creates a new TextField
20 * @param {Object} config Configuration options
22 Roo.form.TextField = function(config){
23 Roo.form.TextField.superclass.constructor.call(this, config);
27 * Fires when the autosize function is triggered. The field may or may not have actually changed size
28 * according to the default logic, but this event provides a hook for the developer to apply additional
29 * logic at runtime to resize the field if needed.
30 * @param {Roo.form.Field} this This text field
31 * @param {Number} width The new field width
37 Roo.extend(Roo.form.TextField, Roo.form.Field, {
39 * @cfg {Boolean} grow True if this field should automatically grow and shrink to its content
43 * @cfg {Number} growMin The minimum width to allow when grow = true (defaults to 30)
47 * @cfg {Number} growMax The maximum width to allow when grow = true (defaults to 800)
51 * @cfg {String} vtype A validation type name as defined in {@link Roo.form.VTypes} (defaults to null)
55 * @cfg {String} maskRe An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null)
59 * @cfg {Boolean} disableKeyFilter True to disable input keystroke filtering (defaults to false)
61 disableKeyFilter : false,
63 * @cfg {Boolean} allowBlank False to validate that the value length > 0 (defaults to true)
67 * @cfg {Number} minLength Minimum input field length required (defaults to 0)
71 * @cfg {Number} maxLength Maximum input field length allowed (defaults to Number.MAX_VALUE)
73 maxLength : Number.MAX_VALUE,
75 * @cfg {String} minLengthText Error text to display if the minimum length validation fails (defaults to "The minimum length for this field is {minLength}")
77 minLengthText : "The minimum length for this field is {0}",
79 * @cfg {String} maxLengthText Error text to display if the maximum length validation fails (defaults to "The maximum length for this field is {maxLength}")
81 maxLengthText : "The maximum length for this field is {0}",
83 * @cfg {Boolean} selectOnFocus True to automatically select any existing field text when the field receives input focus (defaults to false)
85 selectOnFocus : false,
87 * @cfg {String} blankText Error text to display if the allow blank validation fails (defaults to "This field is required")
89 blankText : "This field is required",
91 * @cfg {Function} validator A custom validation function to be called during field validation (defaults to null).
92 * If available, this function will be called only after the basic validators all return true, and will be passed the
93 * current field value and expected to return boolean true if the value is valid or a string error message if invalid.
97 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation (defaults to null).
98 * If available, this regex will be evaluated only after the basic validators all return true, and will be passed the
99 * current field value. If the test fails, the field will be marked invalid using {@link #regexText}.
103 * @cfg {String} regexText The error text to display if {@link #regex} is used and the test fails during validation (defaults to "")
107 * @cfg {String} emptyText The default text to display in an empty field - placeholder... (defaults to null).
111 * @cfg {String} emptyClass The CSS class to apply to an empty field to style the {@link #emptyText} (defaults to
112 * 'x-form-empty-field'). This class is automatically added and removed as needed depending on the current field value.
114 emptyClass : 'x-form-empty-field',
117 initEvents : function()
119 if (this.emptyText) {
120 this.el.attr('placeholder', this.emptyText);
123 Roo.form.TextField.superclass.initEvents.call(this);
124 if(this.validationEvent == 'keyup'){
125 this.validationTask = new Roo.util.DelayedTask(this.validate, this);
126 this.el.on('keyup', this.filterValidation, this);
128 else if(this.validationEvent !== false){
129 this.el.on(this.validationEvent, this.validate, this, {buffer: this.validationDelay});
131 if(this.selectOnFocus || this.emptyText){
132 this.on("focus", this.preFocus, this);
134 this.on('blur', this.postBlur, this);
138 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Roo.form.VTypes[this.vtype+'Mask']))){
139 this.el.on("keypress", this.filterKeys, this);
142 this.el.on("keyup", this.onKeyUp, this, {buffer:50});
143 this.el.on("click", this.autoSize, this);
145 if(this.el.is('input[type=password]') && Roo.isSafari){
146 this.el.on('keydown', this.SafariOnKeyDown, this);
150 processValue : function(value){
151 if(this.stripCharsRe){
152 var newValue = value.replace(this.stripCharsRe, '');
153 if(newValue !== value){
154 this.setRawValue(newValue);
161 filterValidation : function(e){
162 if(!e.isNavKeyPress()){
163 this.validationTask.delay(this.validationDelay);
168 onKeyUp : function(e){
169 if(!e.isNavKeyPress()){
175 * Resets the current field value to the originally-loaded value and clears any validation messages.
176 * Also adds emptyText and emptyClass if the original value was blank.
179 Roo.form.TextField.superclass.reset.call(this);
180 this.applyEmptyText();
183 applyEmptyText : function(){
184 if(this.rendered && this.emptyText && this.getRawValue().length < 1){
186 this.el.addClass(this.emptyClass);
191 preFocus : function(){
194 this.el.removeClass(this.emptyClass);
196 if(this.selectOnFocus){
197 this.el.dom.select();
202 postBlur : function(){
203 this.applyEmptyText();
207 filterKeys : function(e){
209 if(!Roo.isIE && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
212 var c = e.getCharCode(), cc = String.fromCharCode(c);
213 if(Roo.isIE && (e.isSpecialKey() || !cc)){
216 if(!this.maskRe.test(cc)){
221 setValue : function(v){
222 if(this.emptyText && this.el && v !== undefined && v !== null && v !== ''){
223 this.el.removeClass(this.emptyClass);
225 Roo.form.TextField.superclass.setValue.apply(this, arguments);
226 this.applyEmptyText();
231 * Validates a value according to the field's validation rules and marks the field as invalid
232 * if the validation fails
233 * @param {Mixed} value The value to validate
234 * @return {Boolean} True if the value is valid, else false
236 validateValue : function(value){
237 if(value.length < 1) { // if it's blank
242 this.markInvalid(this.blankText);
246 if(value.length < this.minLength){
247 this.markInvalid(String.format(this.minLengthText, this.minLength));
250 if(value.length > this.maxLength){
251 this.markInvalid(String.format(this.maxLengthText, this.maxLength));
255 var vt = Roo.form.VTypes;
256 if(!vt[this.vtype](value, this)){
257 this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);
261 if(typeof this.validator == "function"){
262 var msg = this.validator(value);
264 this.markInvalid(msg);
268 if(this.regex && !this.regex.test(value)){
269 this.markInvalid(this.regexText);
276 * Selects text in this field
277 * @param {Number} start (optional) The index where the selection should start (defaults to 0)
278 * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
280 selectText : function(start, end){
281 var v = this.getRawValue();
283 start = start === undefined ? 0 : start;
284 end = end === undefined ? v.length : end;
286 if(d.setSelectionRange){
287 d.setSelectionRange(start, end);
288 }else if(d.createTextRange){
289 var range = d.createTextRange();
290 range.moveStart("character", start);
291 range.moveEnd("character", v.length-end);
298 * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
299 * This only takes effect if grow = true, and fires the autosize event.
301 autoSize : function(){
302 if(!this.grow || !this.rendered){
306 this.metrics = Roo.util.TextMetrics.createInstance(this.el);
309 var v = el.dom.value;
310 var d = document.createElement('div');
311 d.appendChild(document.createTextNode(v));
315 var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
317 this.fireEvent("autosize", this, w);
321 SafariOnKeyDown : function(event)
323 // this is a workaround for a password hang bug on chrome/ webkit.
325 var isSelectAll = false;
327 if(this.el.dom.selectionEnd > 0){
328 isSelectAll = (this.el.dom.selectionEnd - this.el.dom.selectionStart - this.getValue().length == 0) ? true : false;
330 if(((event.getKey() == 8 || event.getKey() == 46) && this.getValue().length ==1)){ // backspace and delete key
331 event.preventDefault();
336 if(isSelectAll){ // backspace and delete key
338 event.preventDefault();
339 // this is very hacky as keydown always get's upper case.
341 var cc = String.fromCharCode(event.getCharCode());
342 this.setValue( event.shiftKey ? cc : cc.toLowerCase());