Form'n'Field custom data types

Plug-In: Home Documentation Change Log Download
Documentation: Home Form Data types Input types Online demo

Description

A custom data type is a class that checks the validity of the input value and converts the value provided by the HTML control into a specific type. For example a textbox value is a string, but if the data type is declared as an integer, the returned value will be converted into a JavaScript int.

The declaration of a new data type is done with a call to jQuery.iField.registerDataType(dataTypeName, settings).

jQuery.iField.registerDataType settings

  • [optional] getTyped() function that converts the value present in the input into this data type. If omitted, the value is retrieved as provided by the field get() method.
  • [optional] setTyped(value) function that converts the given typed value into the input format. If omitted, the value is set with the field set(value) method.
  • [optional] onValidate() function called when the field value has changed. Must return true if the value can be converted into this data type.

Example

The following example is the standard 'int' data type. It converts a string into an integer and back. The onValidate() method checks with a regular expression that the string value is a number.

jQuery.iField.registerDataType('int',{
   onValidate:function(){
      return (/^\d+$/).test(this.get());
   },
   getTyped:function(){
      if(this.onValidate())
         return parseInt(this.get());
      else
         return undefined;
   },
   setTyped:function(value){
      return this.set(value===undefined?'':value.toString());
   }
});