inputEx - StringField Usage

Basic StringField creation

Use the following code to create a basic inputEx field.

1new inputEx.StringField({parentEl: 'container1'}); 
2  
view plain | print | ?

With a default value

You can set a default value by specifying the 'value' property in the options object.

1new inputEx.StringField({value: 'inputEx rocks', parentEl: 'container2'}); 
view plain | print | ?

Changing the size

You can set the size of the input.

1new inputEx.StringField({size: 40, value: 'size is set to 40 (default is 20)', parentEl: 'container3'}); 
view plain | print | ?

Maximum and minimum length

You can add a maximum/minimum length on string fields :

1new inputEx.StringField({value: '0123456789', showMsg: true, minLength: 3, maxLength: 10, parentEl: 'container4'}); 
view plain | print | ?

Required

If the 'required' property is set, the 'validate' method will return false if the field is empty. In a form, the 'validate' method will be called on each field and will return false if at least one field doesn't validate.

1new inputEx.StringField({required: true, showMsg: true, parentEl: 'container5'}); 
view plain | print | ?

Regular Expression 1

Here is an example on how to check the value with a regular expression. (It is better to use the IntegerField, but this is a simple example.)

1new inputEx.StringField({label: 'Enter your age', regexp: /^[0-9]*$/, parentEl: 'container6'}); 
view plain | print | ?

Regular Expression 2

The basic Field class can use regular expressions to validate the field content. Here is an example with this wonderful email regular expression (note that there is an Email Field class).

This field is invalid
1new inputEx.StringField({showMsg: true,regexp: inputEx.regexps.email, value: 'wrong@email', parentEl: 'container7'}); 
view plain | print | ?

Enabling/Disabling inputs

You can call the methods 'disable' or 'enable' to set the state of the field.

1var field = new inputEx.StringField({value: 'This field is disabled', parentEl: 'container8'}); 
2field.disable(); 
view plain | print | ?

Updated event

How to listen to the updated event :

Log :
1var el = YAHOO.util.Dom.get('container9'); 
2var field = new inputEx.StringField({parentEl: el }); 
3var logDiv = inputEx.cn('div'nullnull"Log :"); 
4el.appendChild(logDiv); 
5field.updatedEvt.subscribe(function(e,params) { 
6    var val = params[0]; 
7    logDiv.innerHTML += "Updated at "+(new Date())+" with value: "+val; 
8}); 
view plain | print | ?

Type invitation

Display a text when the field is empty.

Enter your lastname
1new inputEx.StringField({parentEl: 'container10', typeInvite: 'lastname', description: 'Enter your lastname'}); 
view plain | print | ?

Various options

Config with various options : typeInvite, required, minLength, trim  --  Test setValue and getValue methods

Enter your lastname
1var field1 = new inputEx.StringField({ 
2    parentEl: 'container11',  
3    typeInvite: 'lastname',  
4    description: 'Enter your lastname',  
5    minLength:10,  
6    trim:true// getValue will return a trimed value 
7    required:true,  
8    showMsg:true 
9}); 
10 
11var exampleDiv = YAHOO.util.Dom.get('container11'); 
12 
13var button1 = inputEx.cn('button'nullnull'setValue()'); 
14exampleDiv.appendChild(button1);  
15YAHOO.util.Event.addListener(button1, 'click'function() {  
16    field1.setValue("I've been set by setValue");  
17}); 
18 
19var button2 = inputEx.cn('button'nullnull'getValue()'); 
20exampleDiv.appendChild(button2);  
21YAHOO.util.Event.addListener(button2, 'click'function() {  
22    alert(field1.getValue());  
23}); 
view plain | print | ?

Focus the field

Use the focus method

1var field12 = new inputEx.StringField({parentEl: 'container12'}); 
2 
3var exampleDiv = YAHOO.util.Dom.get('container12'); 
4 
5var button3 = inputEx.cn('button'nullnull'focus()'); 
6exampleDiv.appendChild(button3);  
7YAHOO.util.Event.addListener(button3, 'click'function() { field12.focus(); }); 
view plain | print | ?