3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.form.TextField"></div>/**
15 * @class Ext.form.TextField
16 * @extends Ext.form.Field
17 * <p>Basic text field. Can be used as a direct replacement for traditional text inputs,
18 * or as the base class for more sophisticated input controls (like {@link Ext.form.TextArea}
19 * and {@link Ext.form.ComboBox}).</p>
20 * <p><b><u>Validation</u></b></p>
21 * <p>The validation procedure is described in the documentation for {@link #validateValue}.</p>
22 * <p><b><u>Alter Validation Behavior</u></b></p>
23 * <p>Validation behavior for each field can be configured:</p>
24 * <div class="mdetail-params"><ul>
25 * <li><code>{@link Ext.form.TextField#invalidText invalidText}</code> : the default validation message to
26 * show if any validation step above does not provide a message when invalid</li>
27 * <li><code>{@link Ext.form.TextField#maskRe maskRe}</code> : filter out keystrokes before any validation occurs</li>
28 * <li><code>{@link Ext.form.TextField#stripCharsRe stripCharsRe}</code> : filter characters after being typed in,
29 * but before being validated</li>
30 * <li><code>{@link Ext.form.Field#invalidClass invalidClass}</code> : alternate style when invalid</li>
31 * <li><code>{@link Ext.form.Field#validateOnBlur validateOnBlur}</code>,
32 * <code>{@link Ext.form.Field#validationDelay validationDelay}</code>, and
33 * <code>{@link Ext.form.Field#validationEvent validationEvent}</code> : modify how/when validation is triggered</li>
36 * @constructor Creates a new TextField
37 * @param {Object} config Configuration options
41 Ext.form.TextField = Ext.extend(Ext.form.Field, {
42 <div id="cfg-Ext.form.TextField-vtypeText"></div>/**
43 * @cfg {String} vtypeText A custom error message to display in place of the default message provided
44 * for the <b><code>{@link #vtype}</code></b> currently set for this field (defaults to <tt>''</tt>). <b>Note</b>:
45 * only applies if <b><code>{@link #vtype}</code></b> is set, else ignored.
47 <div id="cfg-Ext.form.TextField-stripCharsRe"></div>/**
48 * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value
49 * before validation (defaults to <tt>null</tt>).
51 <div id="cfg-Ext.form.TextField-grow"></div>/**
52 * @cfg {Boolean} grow <tt>true</tt> if this field should automatically grow and shrink to its content
53 * (defaults to <tt>false</tt>)
56 <div id="cfg-Ext.form.TextField-growMin"></div>/**
57 * @cfg {Number} growMin The minimum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
61 <div id="cfg-Ext.form.TextField-growMax"></div>/**
62 * @cfg {Number} growMax The maximum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
66 <div id="cfg-Ext.form.TextField-vtype"></div>/**
67 * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to <tt>null</tt>)
70 <div id="cfg-Ext.form.TextField-maskRe"></div>/**
71 * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that do
72 * not match (defaults to <tt>null</tt>)
75 <div id="cfg-Ext.form.TextField-disableKeyFilter"></div>/**
76 * @cfg {Boolean} disableKeyFilter Specify <tt>true</tt> to disable input keystroke filtering (defaults
79 disableKeyFilter : false,
80 <div id="cfg-Ext.form.TextField-allowBlank"></div>/**
81 * @cfg {Boolean} allowBlank Specify <tt>false</tt> to validate that the value's length is > 0 (defaults to
85 <div id="cfg-Ext.form.TextField-minLength"></div>/**
86 * @cfg {Number} minLength Minimum input field length required (defaults to <tt>0</tt>)
89 <div id="cfg-Ext.form.TextField-maxLength"></div>/**
90 * @cfg {Number} maxLength Maximum input field length allowed by validation (defaults to Number.MAX_VALUE).
91 * This behavior is intended to provide instant feedback to the user by improving usability to allow pasting
92 * and editing or overtyping and back tracking. To restrict the maximum number of characters that can be
93 * entered into the field use <tt><b>{@link Ext.form.Field#autoCreate autoCreate}</b></tt> to add
94 * any attributes you want to a field, for example:<pre><code>
95 var myField = new Ext.form.NumberField({
99 maxLength: 16, // for validation
100 autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete: 'off', maxlength: '10'}
104 maxLength : Number.MAX_VALUE,
105 <div id="cfg-Ext.form.TextField-minLengthText"></div>/**
106 * @cfg {String} minLengthText Error text to display if the <b><tt>{@link #minLength minimum length}</tt></b>
107 * validation fails (defaults to <tt>'The minimum length for this field is {minLength}'</tt>)
109 minLengthText : 'The minimum length for this field is {0}',
110 <div id="cfg-Ext.form.TextField-maxLengthText"></div>/**
111 * @cfg {String} maxLengthText Error text to display if the <b><tt>{@link #maxLength maximum length}</tt></b>
112 * validation fails (defaults to <tt>'The maximum length for this field is {maxLength}'</tt>)
114 maxLengthText : 'The maximum length for this field is {0}',
115 <div id="cfg-Ext.form.TextField-selectOnFocus"></div>/**
116 * @cfg {Boolean} selectOnFocus <tt>true</tt> to automatically select any existing field text when the field
117 * receives input focus (defaults to <tt>false</tt>)
119 selectOnFocus : false,
120 <div id="cfg-Ext.form.TextField-blankText"></div>/**
121 * @cfg {String} blankText The error text to display if the <b><tt>{@link #allowBlank}</tt></b> validation
122 * fails (defaults to <tt>'This field is required'</tt>)
124 blankText : 'This field is required',
125 <div id="cfg-Ext.form.TextField-validator"></div>/**
126 * @cfg {Function} validator
127 * <p>A custom validation function to be called during field validation ({@link #validateValue})
128 * (defaults to <tt>null</tt>). If specified, this function will be called first, allowing the
129 * developer to override the default validation process.</p>
130 * <br><p>This function will be passed the following Parameters:</p>
131 * <div class="mdetail-params"><ul>
132 * <li><code>value</code>: <i>Mixed</i>
133 * <div class="sub-desc">The current field value</div></li>
135 * <br><p>This function is to Return:</p>
136 * <div class="mdetail-params"><ul>
137 * <li><code>true</code>: <i>Boolean</i>
138 * <div class="sub-desc"><code>true</code> if the value is valid</div></li>
139 * <li><code>msg</code>: <i>String</i>
140 * <div class="sub-desc">An error message if the value is invalid</div></li>
144 <div id="cfg-Ext.form.TextField-regex"></div>/**
145 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation
146 * (defaults to <tt>null</tt>). If the test fails, the field will be marked invalid using
147 * <b><tt>{@link #regexText}</tt></b>.
150 <div id="cfg-Ext.form.TextField-regexText"></div>/**
151 * @cfg {String} regexText The error text to display if <b><tt>{@link #regex}</tt></b> is used and the
152 * test fails during validation (defaults to <tt>''</tt>)
155 <div id="cfg-Ext.form.TextField-emptyText"></div>/**
156 * @cfg {String} emptyText The default text to place into an empty field (defaults to <tt>null</tt>).
157 * <b>Note</b>: that this value will be submitted to the server if this field is enabled and configured
158 * with a {@link #name}.
161 <div id="cfg-Ext.form.TextField-emptyClass"></div>/**
162 * @cfg {String} emptyClass The CSS class to apply to an empty field to style the <b><tt>{@link #emptyText}</tt></b>
163 * (defaults to <tt>'x-form-empty-field'</tt>). This class is automatically added and removed as needed
164 * depending on the current field value.
166 emptyClass : 'x-form-empty-field',
168 <div id="cfg-Ext.form.TextField-enableKeyEvents"></div>/**
169 * @cfg {Boolean} enableKeyEvents <tt>true</tt> to enable the proxying of key events for the HTML input
170 * field (defaults to <tt>false</tt>)
173 initComponent : function(){
174 Ext.form.TextField.superclass.initComponent.call(this);
176 <div id="event-Ext.form.TextField-autosize"></div>/**
178 * Fires when the <tt><b>{@link #autoSize}</b></tt> function is triggered. The field may or
179 * may not have actually changed size according to the default logic, but this event provides
180 * a hook for the developer to apply additional logic at runtime to resize the field if needed.
181 * @param {Ext.form.Field} this This text field
182 * @param {Number} width The new field width
186 <div id="event-Ext.form.TextField-keydown"></div>/**
188 * Keydown input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
190 * @param {Ext.form.TextField} this This text field
191 * @param {Ext.EventObject} e
194 <div id="event-Ext.form.TextField-keyup"></div>/**
196 * Keyup input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
198 * @param {Ext.form.TextField} this This text field
199 * @param {Ext.EventObject} e
202 <div id="event-Ext.form.TextField-keypress"></div>/**
204 * Keypress input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
206 * @param {Ext.form.TextField} this This text field
207 * @param {Ext.EventObject} e
214 initEvents : function(){
215 Ext.form.TextField.superclass.initEvents.call(this);
216 if(this.validationEvent == 'keyup'){
217 this.validationTask = new Ext.util.DelayedTask(this.validate, this);
218 this.mon(this.el, 'keyup', this.filterValidation, this);
220 else if(this.validationEvent !== false && this.validationEvent != 'blur'){
221 this.mon(this.el, this.validationEvent, this.validate, this, {buffer: this.validationDelay});
223 if(this.selectOnFocus || this.emptyText){
224 this.mon(this.el, 'mousedown', this.onMouseDown, this);
227 this.applyEmptyText();
230 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){
231 this.mon(this.el, 'keypress', this.filterKeys, this);
234 this.mon(this.el, 'keyup', this.onKeyUpBuffered, this, {buffer: 50});
235 this.mon(this.el, 'click', this.autoSize, this);
237 if(this.enableKeyEvents){
241 keydown: this.onKeyDown,
242 keypress: this.onKeyPress
247 onMouseDown: function(e){
249 this.mon(this.el, 'mouseup', Ext.emptyFn, this, { single: true, preventDefault: true });
253 processValue : function(value){
254 if(this.stripCharsRe){
255 var newValue = value.replace(this.stripCharsRe, '');
256 if(newValue !== value){
257 this.setRawValue(newValue);
264 filterValidation : function(e){
265 if(!e.isNavKeyPress()){
266 this.validationTask.delay(this.validationDelay);
271 onDisable: function(){
272 Ext.form.TextField.superclass.onDisable.call(this);
274 this.el.dom.unselectable = 'on';
279 onEnable: function(){
280 Ext.form.TextField.superclass.onEnable.call(this);
282 this.el.dom.unselectable = '';
287 onKeyUpBuffered : function(e){
288 if(!e.isNavKeyPress()){
294 onKeyUp : function(e){
295 this.fireEvent('keyup', this, e);
299 onKeyDown : function(e){
300 this.fireEvent('keydown', this, e);
304 onKeyPress : function(e){
305 this.fireEvent('keypress', this, e);
308 <div id="method-Ext.form.TextField-reset"></div>/**
309 * Resets the current field value to the originally-loaded value and clears any validation messages.
310 * Also adds <tt><b>{@link #emptyText}</b></tt> and <tt><b>{@link #emptyClass}</b></tt> if the
311 * original value was blank.
314 Ext.form.TextField.superclass.reset.call(this);
315 this.applyEmptyText();
318 applyEmptyText : function(){
319 if(this.rendered && this.emptyText && this.getRawValue().length < 1 && !this.hasFocus){
320 this.setRawValue(this.emptyText);
321 this.el.addClass(this.emptyClass);
326 preFocus : function(){
329 if(el.dom.value == this.emptyText){
330 this.setRawValue('');
332 el.removeClass(this.emptyClass);
334 if(this.selectOnFocus){
340 postBlur : function(){
341 this.applyEmptyText();
345 filterKeys : function(e){
346 // special keys don't generate charCodes, so leave them alone
347 if(e.ctrlKey || e.isSpecialKey()){
351 if(!this.maskRe.test(String.fromCharCode(e.getCharCode()))){
356 setValue : function(v){
357 if(this.emptyText && this.el && !Ext.isEmpty(v)){
358 this.el.removeClass(this.emptyClass);
360 Ext.form.TextField.superclass.setValue.apply(this, arguments);
361 this.applyEmptyText();
366 <div id="method-Ext.form.TextField-validateValue"></div>/**
367 * <p>Validates a value according to the field's validation rules and marks the field as invalid
368 * if the validation fails. Validation rules are processed in the following order:</p>
369 * <div class="mdetail-params"><ul>
371 * <li><b>1. Field specific validator</b>
372 * <div class="sub-desc">
373 * <p>A validator offers a way to customize and reuse a validation specification.
374 * If a field is configured with a <code>{@link #validator}</code>
375 * function, it will be passed the current field value. The <code>{@link #validator}</code>
376 * function is expected to return either:
377 * <div class="mdetail-params"><ul>
378 * <li>Boolean <tt>true</tt> if the value is valid (validation continues).</li>
379 * <li>a String to represent the invalid message if invalid (validation halts).</li>
383 * <li><b>2. Basic Validation</b>
384 * <div class="sub-desc">
385 * <p>If the <code>{@link #validator}</code> has not halted validation,
386 * basic validation proceeds as follows:</p>
388 * <div class="mdetail-params"><ul>
390 * <li><code>{@link #allowBlank}</code> : (Invalid message =
391 * <code>{@link #emptyText}</code>)<div class="sub-desc">
392 * Depending on the configuration of <code>{@link #allowBlank}</code>, a
393 * blank field will cause validation to halt at this step and return
394 * Boolean true or false accordingly.
397 * <li><code>{@link #minLength}</code> : (Invalid message =
398 * <code>{@link #minLengthText}</code>)<div class="sub-desc">
399 * If the passed value does not satisfy the <code>{@link #minLength}</code>
400 * specified, validation halts.
403 * <li><code>{@link #maxLength}</code> : (Invalid message =
404 * <code>{@link #maxLengthText}</code>)<div class="sub-desc">
405 * If the passed value does not satisfy the <code>{@link #maxLength}</code>
406 * specified, validation halts.
412 * <li><b>3. Preconfigured Validation Types (VTypes)</b>
413 * <div class="sub-desc">
414 * <p>If none of the prior validation steps halts validation, a field
415 * configured with a <code>{@link #vtype}</code> will utilize the
416 * corresponding {@link Ext.form.VTypes VTypes} validation function.
417 * If invalid, either the field's <code>{@link #vtypeText}</code> or
418 * the VTypes vtype Text property will be used for the invalid message.
419 * Keystrokes on the field will be filtered according to the VTypes
420 * vtype Mask property.</p>
423 * <li><b>4. Field specific regex test</b>
424 * <div class="sub-desc">
425 * <p>If none of the prior validation steps halts validation, a field's
426 * configured <code>{@link #regex}</code> test will be processed.
427 * The invalid message for this test is configured with
428 * <code>{@link #regexText}</code>.</p>
431 * @param {Mixed} value The value to validate
432 * @return {Boolean} True if the value is valid, else false
434 validateValue : function(value){
435 if(Ext.isFunction(this.validator)){
436 var msg = this.validator(value);
438 this.markInvalid(msg);
442 if(value.length < 1 || value === this.emptyText){ // if it's blank
447 this.markInvalid(this.blankText);
451 if(value.length < this.minLength){
452 this.markInvalid(String.format(this.minLengthText, this.minLength));
455 if(value.length > this.maxLength){
456 this.markInvalid(String.format(this.maxLengthText, this.maxLength));
460 var vt = Ext.form.VTypes;
461 if(!vt[this.vtype](value, this)){
462 this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);
466 if(this.regex && !this.regex.test(value)){
467 this.markInvalid(this.regexText);
473 <div id="method-Ext.form.TextField-selectText"></div>/**
474 * Selects text in this field
475 * @param {Number} start (optional) The index where the selection should start (defaults to 0)
476 * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
478 selectText : function(start, end){
479 var v = this.getRawValue();
482 start = start === undefined ? 0 : start;
483 end = end === undefined ? v.length : end;
485 if(d.setSelectionRange){
486 d.setSelectionRange(start, end);
487 }else if(d.createTextRange){
488 var range = d.createTextRange();
489 range.moveStart('character', start);
490 range.moveEnd('character', end-v.length);
493 doFocus = Ext.isGecko || Ext.isOpera;
502 <div id="method-Ext.form.TextField-autoSize"></div>/**
503 * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
504 * This only takes effect if <tt><b>{@link #grow}</b> = true</tt>, and fires the {@link #autosize} event.
506 autoSize : function(){
507 if(!this.grow || !this.rendered){
511 this.metrics = Ext.util.TextMetrics.createInstance(this.el);
514 var v = el.dom.value;
515 var d = document.createElement('div');
516 d.appendChild(document.createTextNode(v));
521 var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
523 this.fireEvent('autosize', this, w);
526 onDestroy: function(){
527 if(this.validationTask){
528 this.validationTask.cancel();
529 this.validationTask = null;
531 Ext.form.TextField.superclass.onDestroy.call(this);
534 Ext.reg('textfield', Ext.form.TextField);