3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.TextField"></div>/**
10 * @class Ext.form.TextField
11 * @extends Ext.form.Field
12 * <p>Basic text field. Can be used as a direct replacement for traditional text inputs,
13 * or as the base class for more sophisticated input controls (like {@link Ext.form.TextArea}
14 * and {@link Ext.form.ComboBox}).</p>
15 * <p><b><u>Validation</u></b></p>
16 * <p>The validation procedure is described in the documentation for {@link #validateValue}.</p>
17 * <p><b><u>Alter Validation Behavior</u></b></p>
18 * <p>Validation behavior for each field can be configured:</p>
19 * <div class="mdetail-params"><ul>
20 * <li><code>{@link Ext.form.TextField#invalidText invalidText}</code> : the default validation message to
21 * show if any validation step above does not provide a message when invalid</li>
22 * <li><code>{@link Ext.form.TextField#maskRe maskRe}</code> : filter out keystrokes before any validation occurs</li>
23 * <li><code>{@link Ext.form.TextField#stripCharsRe stripCharsRe}</code> : filter characters after being typed in,
24 * but before being validated</li>
25 * <li><code>{@link Ext.form.Field#invalidClass invalidClass}</code> : alternate style when invalid</li>
26 * <li><code>{@link Ext.form.Field#validateOnBlur validateOnBlur}</code>,
27 * <code>{@link Ext.form.Field#validationDelay validationDelay}</code>, and
28 * <code>{@link Ext.form.Field#validationEvent validationEvent}</code> : modify how/when validation is triggered</li>
31 * @constructor Creates a new TextField
32 * @param {Object} config Configuration options
36 Ext.form.TextField = Ext.extend(Ext.form.Field, {
37 <div id="cfg-Ext.form.TextField-vtypeText"></div>/**
38 * @cfg {String} vtypeText A custom error message to display in place of the default message provided
39 * for the <b><code>{@link #vtype}</code></b> currently set for this field (defaults to <tt>''</tt>). <b>Note</b>:
40 * only applies if <b><code>{@link #vtype}</code></b> is set, else ignored.
42 <div id="cfg-Ext.form.TextField-stripCharsRe"></div>/**
43 * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value
44 * before validation (defaults to <tt>null</tt>).
46 <div id="cfg-Ext.form.TextField-grow"></div>/**
47 * @cfg {Boolean} grow <tt>true</tt> if this field should automatically grow and shrink to its content
48 * (defaults to <tt>false</tt>)
51 <div id="cfg-Ext.form.TextField-growMin"></div>/**
52 * @cfg {Number} growMin The minimum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
56 <div id="cfg-Ext.form.TextField-growMax"></div>/**
57 * @cfg {Number} growMax The maximum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
61 <div id="cfg-Ext.form.TextField-vtype"></div>/**
62 * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to <tt>null</tt>)
65 <div id="cfg-Ext.form.TextField-maskRe"></div>/**
66 * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that do
67 * not match (defaults to <tt>null</tt>)
70 <div id="cfg-Ext.form.TextField-disableKeyFilter"></div>/**
71 * @cfg {Boolean} disableKeyFilter Specify <tt>true</tt> to disable input keystroke filtering (defaults
74 disableKeyFilter : false,
75 <div id="cfg-Ext.form.TextField-allowBlank"></div>/**
76 * @cfg {Boolean} allowBlank Specify <tt>false</tt> to validate that the value's length is > 0 (defaults to
80 <div id="cfg-Ext.form.TextField-minLength"></div>/**
81 * @cfg {Number} minLength Minimum input field length required (defaults to <tt>0</tt>)
84 <div id="cfg-Ext.form.TextField-maxLength"></div>/**
85 * @cfg {Number} maxLength Maximum input field length allowed by validation (defaults to Number.MAX_VALUE).
86 * This behavior is intended to provide instant feedback to the user by improving usability to allow pasting
87 * and editing or overtyping and back tracking. To restrict the maximum number of characters that can be
88 * entered into the field use <tt><b>{@link Ext.form.Field#autoCreate autoCreate}</b></tt> to add
89 * any attributes you want to a field, for example:<pre><code>
90 var myField = new Ext.form.NumberField({
94 maxLength: 16, // for validation
95 autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete: 'off', maxlength: '10'}
99 maxLength : Number.MAX_VALUE,
100 <div id="cfg-Ext.form.TextField-minLengthText"></div>/**
101 * @cfg {String} minLengthText Error text to display if the <b><tt>{@link #minLength minimum length}</tt></b>
102 * validation fails (defaults to <tt>'The minimum length for this field is {minLength}'</tt>)
104 minLengthText : 'The minimum length for this field is {0}',
105 <div id="cfg-Ext.form.TextField-maxLengthText"></div>/**
106 * @cfg {String} maxLengthText Error text to display if the <b><tt>{@link #maxLength maximum length}</tt></b>
107 * validation fails (defaults to <tt>'The maximum length for this field is {maxLength}'</tt>)
109 maxLengthText : 'The maximum length for this field is {0}',
110 <div id="cfg-Ext.form.TextField-selectOnFocus"></div>/**
111 * @cfg {Boolean} selectOnFocus <tt>true</tt> to automatically select any existing field text when the field
112 * receives input focus (defaults to <tt>false</tt>)
114 selectOnFocus : false,
115 <div id="cfg-Ext.form.TextField-blankText"></div>/**
116 * @cfg {String} blankText The error text to display if the <b><tt>{@link #allowBlank}</tt></b> validation
117 * fails (defaults to <tt>'This field is required'</tt>)
119 blankText : 'This field is required',
120 <div id="cfg-Ext.form.TextField-validator"></div>/**
121 * @cfg {Function} validator
122 * <p>A custom validation function to be called during field validation ({@link #validateValue})
123 * (defaults to <tt>null</tt>). If specified, this function will be called first, allowing the
124 * developer to override the default validation process.</p>
125 * <br><p>This function will be passed the following Parameters:</p>
126 * <div class="mdetail-params"><ul>
127 * <li><code>value</code>: <i>Mixed</i>
128 * <div class="sub-desc">The current field value</div></li>
130 * <br><p>This function is to Return:</p>
131 * <div class="mdetail-params"><ul>
132 * <li><code>true</code>: <i>Boolean</i>
133 * <div class="sub-desc"><code>true</code> if the value is valid</div></li>
134 * <li><code>msg</code>: <i>String</i>
135 * <div class="sub-desc">An error message if the value is invalid</div></li>
139 <div id="cfg-Ext.form.TextField-regex"></div>/**
140 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation
141 * (defaults to <tt>null</tt>). If the test fails, the field will be marked invalid using
142 * <b><tt>{@link #regexText}</tt></b>.
145 <div id="cfg-Ext.form.TextField-regexText"></div>/**
146 * @cfg {String} regexText The error text to display if <b><tt>{@link #regex}</tt></b> is used and the
147 * test fails during validation (defaults to <tt>''</tt>)
150 <div id="cfg-Ext.form.TextField-emptyText"></div>/**
151 * @cfg {String} emptyText The default text to place into an empty field (defaults to <tt>null</tt>).
152 * <b>Note</b>: that this value will be submitted to the server if this field is enabled and configured
153 * with a {@link #name}.
156 <div id="cfg-Ext.form.TextField-emptyClass"></div>/**
157 * @cfg {String} emptyClass The CSS class to apply to an empty field to style the <b><tt>{@link #emptyText}</tt></b>
158 * (defaults to <tt>'x-form-empty-field'</tt>). This class is automatically added and removed as needed
159 * depending on the current field value.
161 emptyClass : 'x-form-empty-field',
163 <div id="cfg-Ext.form.TextField-enableKeyEvents"></div>/**
164 * @cfg {Boolean} enableKeyEvents <tt>true</tt> to enable the proxying of key events for the HTML input
165 * field (defaults to <tt>false</tt>)
168 initComponent : function(){
169 Ext.form.TextField.superclass.initComponent.call(this);
171 <div id="event-Ext.form.TextField-autosize"></div>/**
173 * Fires when the <tt><b>{@link #autoSize}</b></tt> function is triggered. The field may or
174 * may not have actually changed size according to the default logic, but this event provides
175 * a hook for the developer to apply additional logic at runtime to resize the field if needed.
176 * @param {Ext.form.Field} this This text field
177 * @param {Number} width The new field width
181 <div id="event-Ext.form.TextField-keydown"></div>/**
183 * Keydown input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
185 * @param {Ext.form.TextField} this This text field
186 * @param {Ext.EventObject} e
189 <div id="event-Ext.form.TextField-keyup"></div>/**
191 * Keyup input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
193 * @param {Ext.form.TextField} this This text field
194 * @param {Ext.EventObject} e
197 <div id="event-Ext.form.TextField-keypress"></div>/**
199 * Keypress input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
201 * @param {Ext.form.TextField} this This text field
202 * @param {Ext.EventObject} e
209 initEvents : function(){
210 Ext.form.TextField.superclass.initEvents.call(this);
211 if(this.validationEvent == 'keyup'){
212 this.validationTask = new Ext.util.DelayedTask(this.validate, this);
213 this.mon(this.el, 'keyup', this.filterValidation, this);
215 else if(this.validationEvent !== false && this.validationEvent != 'blur'){
216 this.mon(this.el, this.validationEvent, this.validate, this, {buffer: this.validationDelay});
218 if(this.selectOnFocus || this.emptyText){
219 this.mon(this.el, 'mousedown', this.onMouseDown, this);
222 this.applyEmptyText();
225 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){
226 this.mon(this.el, 'keypress', this.filterKeys, this);
229 this.mon(this.el, 'keyup', this.onKeyUpBuffered, this, {buffer: 50});
230 this.mon(this.el, 'click', this.autoSize, this);
232 if(this.enableKeyEvents){
236 keydown: this.onKeyDown,
237 keypress: this.onKeyPress
242 onMouseDown: function(e){
244 this.mon(this.el, 'mouseup', Ext.emptyFn, this, { single: true, preventDefault: true });
248 processValue : function(value){
249 if(this.stripCharsRe){
250 var newValue = value.replace(this.stripCharsRe, '');
251 if(newValue !== value){
252 this.setRawValue(newValue);
259 filterValidation : function(e){
260 if(!e.isNavKeyPress()){
261 this.validationTask.delay(this.validationDelay);
266 onDisable: function(){
267 Ext.form.TextField.superclass.onDisable.call(this);
269 this.el.dom.unselectable = 'on';
274 onEnable: function(){
275 Ext.form.TextField.superclass.onEnable.call(this);
277 this.el.dom.unselectable = '';
282 onKeyUpBuffered : function(e){
283 if(this.doAutoSize(e)){
289 doAutoSize : function(e){
290 return !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){
350 if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
353 var cc = String.fromCharCode(e.getCharCode());
354 if(!Ext.isGecko && e.isSpecialKey() && !cc){
357 if(!this.maskRe.test(cc)){
362 setValue : function(v){
363 if(this.emptyText && this.el && !Ext.isEmpty(v)){
364 this.el.removeClass(this.emptyClass);
366 Ext.form.TextField.superclass.setValue.apply(this, arguments);
367 this.applyEmptyText();
372 <div id="method-Ext.form.TextField-validateValue"></div>/**
373 * <p>Validates a value according to the field's validation rules and marks the field as invalid
374 * if the validation fails. Validation rules are processed in the following order:</p>
375 * <div class="mdetail-params"><ul>
377 * <li><b>1. Field specific validator</b>
378 * <div class="sub-desc">
379 * <p>A validator offers a way to customize and reuse a validation specification.
380 * If a field is configured with a <code>{@link #validator}</code>
381 * function, it will be passed the current field value. The <code>{@link #validator}</code>
382 * function is expected to return either:
383 * <div class="mdetail-params"><ul>
384 * <li>Boolean <tt>true</tt> if the value is valid (validation continues).</li>
385 * <li>a String to represent the invalid message if invalid (validation halts).</li>
389 * <li><b>2. Basic Validation</b>
390 * <div class="sub-desc">
391 * <p>If the <code>{@link #validator}</code> has not halted validation,
392 * basic validation proceeds as follows:</p>
394 * <div class="mdetail-params"><ul>
396 * <li><code>{@link #allowBlank}</code> : (Invalid message =
397 * <code>{@link #emptyText}</code>)<div class="sub-desc">
398 * Depending on the configuration of <code>{@link #allowBlank}</code>, a
399 * blank field will cause validation to halt at this step and return
400 * Boolean true or false accordingly.
403 * <li><code>{@link #minLength}</code> : (Invalid message =
404 * <code>{@link #minLengthText}</code>)<div class="sub-desc">
405 * If the passed value does not satisfy the <code>{@link #minLength}</code>
406 * specified, validation halts.
409 * <li><code>{@link #maxLength}</code> : (Invalid message =
410 * <code>{@link #maxLengthText}</code>)<div class="sub-desc">
411 * If the passed value does not satisfy the <code>{@link #maxLength}</code>
412 * specified, validation halts.
418 * <li><b>3. Preconfigured Validation Types (VTypes)</b>
419 * <div class="sub-desc">
420 * <p>If none of the prior validation steps halts validation, a field
421 * configured with a <code>{@link #vtype}</code> will utilize the
422 * corresponding {@link Ext.form.VTypes VTypes} validation function.
423 * If invalid, either the field's <code>{@link #vtypeText}</code> or
424 * the VTypes vtype Text property will be used for the invalid message.
425 * Keystrokes on the field will be filtered according to the VTypes
426 * vtype Mask property.</p>
429 * <li><b>4. Field specific regex test</b>
430 * <div class="sub-desc">
431 * <p>If none of the prior validation steps halts validation, a field's
432 * configured <code>{@link #regex}</code> test will be processed.
433 * The invalid message for this test is configured with
434 * <code>{@link #regexText}</code>.</p>
437 * @param {Mixed} value The value to validate
438 * @return {Boolean} True if the value is valid, else false
440 validateValue : function(value){
441 if(Ext.isFunction(this.validator)){
442 var msg = this.validator(value);
444 this.markInvalid(msg);
448 if(value.length < 1 || value === this.emptyText){ // if it's blank
453 this.markInvalid(this.blankText);
457 if(value.length < this.minLength){
458 this.markInvalid(String.format(this.minLengthText, this.minLength));
461 if(value.length > this.maxLength){
462 this.markInvalid(String.format(this.maxLengthText, this.maxLength));
466 var vt = Ext.form.VTypes;
467 if(!vt[this.vtype](value, this)){
468 this.markInvalid(this.vtypeText || vt[this.vtype +'Text']);
472 if(this.regex && !this.regex.test(value)){
473 this.markInvalid(this.regexText);
479 <div id="method-Ext.form.TextField-selectText"></div>/**
480 * Selects text in this field
481 * @param {Number} start (optional) The index where the selection should start (defaults to 0)
482 * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
484 selectText : function(start, end){
485 var v = this.getRawValue();
488 start = start === undefined ? 0 : start;
489 end = end === undefined ? v.length : end;
491 if(d.setSelectionRange){
492 d.setSelectionRange(start, end);
493 }else if(d.createTextRange){
494 var range = d.createTextRange();
495 range.moveStart('character', start);
496 range.moveEnd('character', end-v.length);
499 doFocus = Ext.isGecko || Ext.isOpera;
508 <div id="method-Ext.form.TextField-autoSize"></div>/**
509 * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
510 * This only takes effect if <tt><b>{@link #grow}</b> = true</tt>, and fires the {@link #autosize} event.
512 autoSize : function(){
513 if(!this.grow || !this.rendered){
517 this.metrics = Ext.util.TextMetrics.createInstance(this.el);
520 var v = el.dom.value;
521 var d = document.createElement('div');
522 d.appendChild(document.createTextNode(v));
527 var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
529 this.fireEvent('autosize', this, w);
532 onDestroy: function(){
533 if(this.validationTask){
534 this.validationTask.cancel();
535 this.validationTask = null;
537 Ext.form.TextField.superclass.onDestroy.call(this);
540 Ext.reg('textfield', Ext.form.TextField);