3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.form.TextField"></div>/**
16 * @class Ext.form.TextField
17 * @extends Ext.form.Field
18 * <p>Basic text field. Can be used as a direct replacement for traditional text inputs,
19 * or as the base class for more sophisticated input controls (like {@link Ext.form.TextArea}
20 * and {@link Ext.form.ComboBox}).</p>
21 * <p><b><u>Validation</u></b></p>
22 * <p>The validation procedure is described in the documentation for {@link #validateValue}.</p>
23 * <p><b><u>Alter Validation Behavior</u></b></p>
24 * <p>Validation behavior for each field can be configured:</p>
25 * <div class="mdetail-params"><ul>
26 * <li><code>{@link Ext.form.TextField#invalidText invalidText}</code> : the default validation message to
27 * show if any validation step above does not provide a message when invalid</li>
28 * <li><code>{@link Ext.form.TextField#maskRe maskRe}</code> : filter out keystrokes before any validation occurs</li>
29 * <li><code>{@link Ext.form.TextField#stripCharsRe stripCharsRe}</code> : filter characters after being typed in,
30 * but before being validated</li>
31 * <li><code>{@link Ext.form.Field#invalidClass invalidClass}</code> : alternate style when invalid</li>
32 * <li><code>{@link Ext.form.Field#validateOnBlur validateOnBlur}</code>,
33 * <code>{@link Ext.form.Field#validationDelay validationDelay}</code>, and
34 * <code>{@link Ext.form.Field#validationEvent validationEvent}</code> : modify how/when validation is triggered</li>
37 * @constructor Creates a new TextField
38 * @param {Object} config Configuration options
42 Ext.form.TextField = Ext.extend(Ext.form.Field, {
43 <div id="cfg-Ext.form.TextField-vtypeText"></div>/**
44 * @cfg {String} vtypeText A custom error message to display in place of the default message provided
45 * for the <b><code>{@link #vtype}</code></b> currently set for this field (defaults to <tt>''</tt>). <b>Note</b>:
46 * only applies if <b><code>{@link #vtype}</code></b> is set, else ignored.
48 <div id="cfg-Ext.form.TextField-stripCharsRe"></div>/**
49 * @cfg {RegExp} stripCharsRe A JavaScript RegExp object used to strip unwanted content from the value
50 * before validation (defaults to <tt>null</tt>).
52 <div id="cfg-Ext.form.TextField-grow"></div>/**
53 * @cfg {Boolean} grow <tt>true</tt> if this field should automatically grow and shrink to its content
54 * (defaults to <tt>false</tt>)
57 <div id="cfg-Ext.form.TextField-growMin"></div>/**
58 * @cfg {Number} growMin The minimum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
62 <div id="cfg-Ext.form.TextField-growMax"></div>/**
63 * @cfg {Number} growMax The maximum width to allow when <code><b>{@link #grow}</b> = true</code> (defaults
67 <div id="cfg-Ext.form.TextField-vtype"></div>/**
68 * @cfg {String} vtype A validation type name as defined in {@link Ext.form.VTypes} (defaults to <tt>null</tt>)
71 <div id="cfg-Ext.form.TextField-maskRe"></div>/**
72 * @cfg {RegExp} maskRe An input mask regular expression that will be used to filter keystrokes that do
73 * not match (defaults to <tt>null</tt>)
76 <div id="cfg-Ext.form.TextField-disableKeyFilter"></div>/**
77 * @cfg {Boolean} disableKeyFilter Specify <tt>true</tt> to disable input keystroke filtering (defaults
80 disableKeyFilter : false,
81 <div id="cfg-Ext.form.TextField-allowBlank"></div>/**
82 * @cfg {Boolean} allowBlank Specify <tt>false</tt> to validate that the value's length is > 0 (defaults to
86 <div id="cfg-Ext.form.TextField-minLength"></div>/**
87 * @cfg {Number} minLength Minimum input field length required (defaults to <tt>0</tt>)
90 <div id="cfg-Ext.form.TextField-maxLength"></div>/**
91 * @cfg {Number} maxLength Maximum input field length allowed by validation (defaults to Number.MAX_VALUE).
92 * This behavior is intended to provide instant feedback to the user by improving usability to allow pasting
93 * and editing or overtyping and back tracking. To restrict the maximum number of characters that can be
94 * entered into the field use <tt><b>{@link Ext.form.Field#autoCreate autoCreate}</b></tt> to add
95 * any attributes you want to a field, for example:<pre><code>
96 var myField = new Ext.form.NumberField({
100 maxLength: 16, // for validation
101 autoCreate: {tag: 'input', type: 'text', size: '20', autocomplete: 'off', maxlength: '10'}
105 maxLength : Number.MAX_VALUE,
106 <div id="cfg-Ext.form.TextField-minLengthText"></div>/**
107 * @cfg {String} minLengthText Error text to display if the <b><tt>{@link #minLength minimum length}</tt></b>
108 * validation fails (defaults to <tt>'The minimum length for this field is {minLength}'</tt>)
110 minLengthText : 'The minimum length for this field is {0}',
111 <div id="cfg-Ext.form.TextField-maxLengthText"></div>/**
112 * @cfg {String} maxLengthText Error text to display if the <b><tt>{@link #maxLength maximum length}</tt></b>
113 * validation fails (defaults to <tt>'The maximum length for this field is {maxLength}'</tt>)
115 maxLengthText : 'The maximum length for this field is {0}',
116 <div id="cfg-Ext.form.TextField-selectOnFocus"></div>/**
117 * @cfg {Boolean} selectOnFocus <tt>true</tt> to automatically select any existing field text when the field
118 * receives input focus (defaults to <tt>false</tt>)
120 selectOnFocus : false,
121 <div id="cfg-Ext.form.TextField-blankText"></div>/**
122 * @cfg {String} blankText The error text to display if the <b><tt>{@link #allowBlank}</tt></b> validation
123 * fails (defaults to <tt>'This field is required'</tt>)
125 blankText : 'This field is required',
126 <div id="cfg-Ext.form.TextField-validator"></div>/**
127 * @cfg {Function} validator
128 * <p>A custom validation function to be called during field validation ({@link #validateValue})
129 * (defaults to <tt>null</tt>). If specified, this function will be called first, allowing the
130 * developer to override the default validation process.</p>
131 * <br><p>This function will be passed the following Parameters:</p>
132 * <div class="mdetail-params"><ul>
133 * <li><code>value</code>: <i>Mixed</i>
134 * <div class="sub-desc">The current field value</div></li>
136 * <br><p>This function is to Return:</p>
137 * <div class="mdetail-params"><ul>
138 * <li><code>true</code>: <i>Boolean</i>
139 * <div class="sub-desc"><code>true</code> if the value is valid</div></li>
140 * <li><code>msg</code>: <i>String</i>
141 * <div class="sub-desc">An error message if the value is invalid</div></li>
145 <div id="cfg-Ext.form.TextField-regex"></div>/**
146 * @cfg {RegExp} regex A JavaScript RegExp object to be tested against the field value during validation
147 * (defaults to <tt>null</tt>). If the test fails, the field will be marked invalid using
148 * <b><tt>{@link #regexText}</tt></b>.
151 <div id="cfg-Ext.form.TextField-regexText"></div>/**
152 * @cfg {String} regexText The error text to display if <b><tt>{@link #regex}</tt></b> is used and the
153 * test fails during validation (defaults to <tt>''</tt>)
156 <div id="cfg-Ext.form.TextField-emptyText"></div>/**
157 * @cfg {String} emptyText The default text to place into an empty field (defaults to <tt>null</tt>).
158 * <b>Note</b>: that this value will be submitted to the server if this field is enabled and configured
159 * with a {@link #name}.
162 <div id="cfg-Ext.form.TextField-emptyClass"></div>/**
163 * @cfg {String} emptyClass The CSS class to apply to an empty field to style the <b><tt>{@link #emptyText}</tt></b>
164 * (defaults to <tt>'x-form-empty-field'</tt>). This class is automatically added and removed as needed
165 * depending on the current field value.
167 emptyClass : 'x-form-empty-field',
169 <div id="cfg-Ext.form.TextField-enableKeyEvents"></div>/**
170 * @cfg {Boolean} enableKeyEvents <tt>true</tt> to enable the proxying of key events for the HTML input
171 * field (defaults to <tt>false</tt>)
174 initComponent : function(){
175 Ext.form.TextField.superclass.initComponent.call(this);
177 <div id="event-Ext.form.TextField-autosize"></div>/**
179 * Fires when the <tt><b>{@link #autoSize}</b></tt> function is triggered. The field may or
180 * may not have actually changed size according to the default logic, but this event provides
181 * a hook for the developer to apply additional logic at runtime to resize the field if needed.
182 * @param {Ext.form.Field} this This text field
183 * @param {Number} width The new field width
187 <div id="event-Ext.form.TextField-keydown"></div>/**
189 * Keydown input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
191 * @param {Ext.form.TextField} this This text field
192 * @param {Ext.EventObject} e
195 <div id="event-Ext.form.TextField-keyup"></div>/**
197 * Keyup input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
199 * @param {Ext.form.TextField} this This text field
200 * @param {Ext.EventObject} e
203 <div id="event-Ext.form.TextField-keypress"></div>/**
205 * Keypress input field event. This event only fires if <tt><b>{@link #enableKeyEvents}</b></tt>
207 * @param {Ext.form.TextField} this This text field
208 * @param {Ext.EventObject} e
215 initEvents : function(){
216 Ext.form.TextField.superclass.initEvents.call(this);
217 if(this.validationEvent == 'keyup'){
218 this.validationTask = new Ext.util.DelayedTask(this.validate, this);
219 this.mon(this.el, 'keyup', this.filterValidation, this);
221 else if(this.validationEvent !== false && this.validationEvent != 'blur'){
222 this.mon(this.el, this.validationEvent, this.validate, this, {buffer: this.validationDelay});
224 if(this.selectOnFocus || this.emptyText){
225 this.mon(this.el, 'mousedown', this.onMouseDown, this);
228 this.applyEmptyText();
231 if(this.maskRe || (this.vtype && this.disableKeyFilter !== true && (this.maskRe = Ext.form.VTypes[this.vtype+'Mask']))){
232 this.mon(this.el, 'keypress', this.filterKeys, this);
235 this.mon(this.el, 'keyup', this.onKeyUpBuffered, this, {buffer: 50});
236 this.mon(this.el, 'click', this.autoSize, this);
238 if(this.enableKeyEvents){
242 keydown: this.onKeyDown,
243 keypress: this.onKeyPress
248 onMouseDown: function(e){
250 this.mon(this.el, 'mouseup', Ext.emptyFn, this, { single: true, preventDefault: true });
254 processValue : function(value){
255 if(this.stripCharsRe){
256 var newValue = value.replace(this.stripCharsRe, '');
257 if(newValue !== value){
258 this.setRawValue(newValue);
265 filterValidation : function(e){
266 if(!e.isNavKeyPress()){
267 this.validationTask.delay(this.validationDelay);
272 onDisable: function(){
273 Ext.form.TextField.superclass.onDisable.call(this);
275 this.el.dom.unselectable = 'on';
280 onEnable: function(){
281 Ext.form.TextField.superclass.onEnable.call(this);
283 this.el.dom.unselectable = '';
288 onKeyUpBuffered : function(e){
289 if(this.doAutoSize(e)){
295 doAutoSize : function(e){
296 return !e.isNavKeyPress();
300 onKeyUp : function(e){
301 this.fireEvent('keyup', this, e);
305 onKeyDown : function(e){
306 this.fireEvent('keydown', this, e);
310 onKeyPress : function(e){
311 this.fireEvent('keypress', this, e);
314 <div id="method-Ext.form.TextField-reset"></div>/**
315 * Resets the current field value to the originally-loaded value and clears any validation messages.
316 * Also adds <tt><b>{@link #emptyText}</b></tt> and <tt><b>{@link #emptyClass}</b></tt> if the
317 * original value was blank.
320 Ext.form.TextField.superclass.reset.call(this);
321 this.applyEmptyText();
324 applyEmptyText : function(){
325 if(this.rendered && this.emptyText && this.getRawValue().length < 1 && !this.hasFocus){
326 this.setRawValue(this.emptyText);
327 this.el.addClass(this.emptyClass);
332 preFocus : function(){
336 if(el.dom.value == this.emptyText){
337 this.setRawValue('');
340 el.removeClass(this.emptyClass);
342 if(this.selectOnFocus || isEmpty){
348 postBlur : function(){
349 this.applyEmptyText();
353 filterKeys : function(e){
358 if(Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))){
361 var cc = String.fromCharCode(e.getCharCode());
362 if(!Ext.isGecko && e.isSpecialKey() && !cc){
365 if(!this.maskRe.test(cc)){
370 setValue : function(v){
371 if(this.emptyText && this.el && !Ext.isEmpty(v)){
372 this.el.removeClass(this.emptyClass);
374 Ext.form.TextField.superclass.setValue.apply(this, arguments);
375 this.applyEmptyText();
380 <div id="method-Ext.form.TextField-getErrors"></div>/**
381 * <p>Validates a value according to the field's validation rules and returns an array of errors
382 * for any failing validations. Validation rules are processed in the following order:</p>
383 * <div class="mdetail-params"><ul>
385 * <li><b>1. Field specific validator</b>
386 * <div class="sub-desc">
387 * <p>A validator offers a way to customize and reuse a validation specification.
388 * If a field is configured with a <code>{@link #validator}</code>
389 * function, it will be passed the current field value. The <code>{@link #validator}</code>
390 * function is expected to return either:
391 * <div class="mdetail-params"><ul>
392 * <li>Boolean <tt>true</tt> if the value is valid (validation continues).</li>
393 * <li>a String to represent the invalid message if invalid (validation halts).</li>
397 * <li><b>2. Basic Validation</b>
398 * <div class="sub-desc">
399 * <p>If the <code>{@link #validator}</code> has not halted validation,
400 * basic validation proceeds as follows:</p>
402 * <div class="mdetail-params"><ul>
404 * <li><code>{@link #allowBlank}</code> : (Invalid message =
405 * <code>{@link #emptyText}</code>)<div class="sub-desc">
406 * Depending on the configuration of <code>{@link #allowBlank}</code>, a
407 * blank field will cause validation to halt at this step and return
408 * Boolean true or false accordingly.
411 * <li><code>{@link #minLength}</code> : (Invalid message =
412 * <code>{@link #minLengthText}</code>)<div class="sub-desc">
413 * If the passed value does not satisfy the <code>{@link #minLength}</code>
414 * specified, validation halts.
417 * <li><code>{@link #maxLength}</code> : (Invalid message =
418 * <code>{@link #maxLengthText}</code>)<div class="sub-desc">
419 * If the passed value does not satisfy the <code>{@link #maxLength}</code>
420 * specified, validation halts.
426 * <li><b>3. Preconfigured Validation Types (VTypes)</b>
427 * <div class="sub-desc">
428 * <p>If none of the prior validation steps halts validation, a field
429 * configured with a <code>{@link #vtype}</code> will utilize the
430 * corresponding {@link Ext.form.VTypes VTypes} validation function.
431 * If invalid, either the field's <code>{@link #vtypeText}</code> or
432 * the VTypes vtype Text property will be used for the invalid message.
433 * Keystrokes on the field will be filtered according to the VTypes
434 * vtype Mask property.</p>
437 * <li><b>4. Field specific regex test</b>
438 * <div class="sub-desc">
439 * <p>If none of the prior validation steps halts validation, a field's
440 * configured <code>{@link #regex}</code> test will be processed.
441 * The invalid message for this test is configured with
442 * <code>{@link #regexText}</code>.</p>
445 * @param {Mixed} value The value to validate. The processed raw value will be used if nothing is passed
446 * @return {Array} Array of any validation errors
448 getErrors: function(value) {
449 var errors = Ext.form.TextField.superclass.getErrors.apply(this, arguments);
451 value = Ext.isDefined(value) ? value : this.processValue(this.getRawValue());
453 if (Ext.isFunction(this.validator)) {
454 var msg = this.validator(value);
460 if (value.length < 1 || value === this.emptyText) {
461 if (this.allowBlank) {
462 //if value is blank and allowBlank is true, there cannot be any additional errors
465 errors.push(this.blankText);
469 if (!this.allowBlank && (value.length < 1 || value === this.emptyText)) { // if it's blank
470 errors.push(this.blankText);
473 if (value.length < this.minLength) {
474 errors.push(String.format(this.minLengthText, this.minLength));
477 if (value.length > this.maxLength) {
478 errors.push(String.format(this.maxLengthText, this.maxLength));
482 var vt = Ext.form.VTypes;
483 if(!vt[this.vtype](value, this)){
484 errors.push(this.vtypeText || vt[this.vtype +'Text']);
488 if (this.regex && !this.regex.test(value)) {
489 errors.push(this.regexText);
495 <div id="method-Ext.form.TextField-selectText"></div>/**
496 * Selects text in this field
497 * @param {Number} start (optional) The index where the selection should start (defaults to 0)
498 * @param {Number} end (optional) The index where the selection should end (defaults to the text length)
500 selectText : function(start, end){
501 var v = this.getRawValue();
504 start = start === undefined ? 0 : start;
505 end = end === undefined ? v.length : end;
507 if(d.setSelectionRange){
508 d.setSelectionRange(start, end);
509 }else if(d.createTextRange){
510 var range = d.createTextRange();
511 range.moveStart('character', start);
512 range.moveEnd('character', end-v.length);
515 doFocus = Ext.isGecko || Ext.isOpera;
524 <div id="method-Ext.form.TextField-autoSize"></div>/**
525 * Automatically grows the field to accomodate the width of the text up to the maximum field width allowed.
526 * This only takes effect if <tt><b>{@link #grow}</b> = true</tt>, and fires the {@link #autosize} event.
528 autoSize : function(){
529 if(!this.grow || !this.rendered){
533 this.metrics = Ext.util.TextMetrics.createInstance(this.el);
536 var v = el.dom.value;
537 var d = document.createElement('div');
538 d.appendChild(document.createTextNode(v));
543 var w = Math.min(this.growMax, Math.max(this.metrics.getWidth(v) + /* add extra padding */ 10, this.growMin));
545 this.fireEvent('autosize', this, w);
548 onDestroy: function(){
549 if(this.validationTask){
550 this.validationTask.cancel();
551 this.validationTask = null;
553 Ext.form.TextField.superclass.onDestroy.call(this);
556 Ext.reg('textfield', Ext.form.TextField);