inputEx - StringField Usage

Basic StringField creation

Use the following code to create a basic inputEx field.

		new Y.inputEx.StringField({parentEl: 'container1'});

With a default value

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

			new Y.inputEx.StringField({value: 'inputEx rocks', parentEl: 'container2'});
		

Changing the size

You can set the size of the input.

		new Y.inputEx.StringField({size: 40, value: 'size is set to 40 (default is 20)', parentEl: 'container3'});
		

Maximum and minimum length

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

			new Y.inputEx.StringField({value: '0123456789', showMsg: true, minLength: 3, maxLength: 10, parentEl: 'container4'});
		

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.

			new Y.inputEx.StringField({required: true, showMsg: true, parentEl: 'container5'});
		

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.)

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

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).

		new Y.inputEx.StringField({showMsg: true,regexp: inputEx.regexps.email, value: 'wrong@email', parentEl: 'container7'});
		

Enabling/Disabling inputs

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

			var field = new Y.inputEx.StringField({value: 'This field is disabled', parentEl: 'container8'});
			field.disable();
		

Updated event

How to listen to the updated event :

					var el = Y.one('#container9');
					var field = new Y.inputEx.StringField({parentEl: el });
					var logDiv = Y.inputEx.cn('div', null, null, "Log :");
					el.appendChild(logDiv);
					field.on("updated",function(val) {
						logDiv.innerHTML += "Updated at "+(new Date())+" with value: "+val;
					});
		

Type invitation

Display a text when the field is empty.

		new Y.inputEx.StringField({parentEl: 'container10', typeInvite: 'lastname', description: 'Enter your lastname'});
		

Various options

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

			var field1 = new Y.inputEx.StringField({
				parentEl: 'container11', 
				typeInvite: 'lastname', 
				description: 'Enter your lastname', 
				minLength:10, 
				trim:true, // getValue will return a trimed value
				required:true, 
				showMsg:true
			});

			var exampleDiv = Y.one('#container11');

			var button1 = Y.inputEx.cn('button', null, null, 'setValue()');
			exampleDiv.appendChild(button1); 
			Y.on('click', function() { 
				field1.setValue("I've been set by setValue"); 
			}, button1);

			var button2 = Y.inputEx.cn('button', null, null, 'getValue()');
			exampleDiv.appendChild(button2); 
			Y.on( 'click', function() { 
				alert(field1.getValue()); 
			},button2);
		

Focus the field

Use the focus method

			var field12 = new Y.inputEx.StringField({parentEl: 'container12'});

			var exampleDiv = Y.one('#container12');

			var button3 = Y.inputEx.cn('button', null, null, 'focus()');
			exampleDiv.appendChild(button3); 
			Y.on('click', function() { field12.focus(); }, button3);