Validators

use fieldXValidators=”empty,minchar(2),…” to add validators to the field. #js: supported.

  • empty: checks if the field is empty
  • minchar(n): checks if the field contains at least n chars.
  • maxchar(n): checks if the field contains at most n chars.
  • min(value): checks if the field’s value is lower than the given value. Works with time values, too.
  • max(value): checks if the field’s value is higher than the given value. Works with time values, too.
  • number: checks if the field contains a number.
  • regex(regex): checks if the field matches the regular expression.

You can use the validators independent from the section_fieldgroup require tag anywhere in your Titanium app as follows:

var validators = require("jbuss/validators");

validators.reset(); // make sure validator list is empty
validators.add($.inputLastname, "empty", "lastname", {msg: "give a value for lastname please"});
validators.add($.inputLastname, "minchar", "lastname"), {chars: 2, msg: "at least 2 chars for lastname please"});

if (!validators.runAll({allowAlert: true})) return;

with validators.add(field, validator, field label, options):

  • field the field the validator should work on
  • validator the validator type (empty, minchar…)
  • field label the label for the field
  • optional options.msg a custom message when the validation fails
  • options.chars number of chars for the minchar or maxchar validator
  • options.regex regular expression for the regex validator
  • options.min the compare value for the min validator
  • options.max the compare vlaue for the max validator
  • optional options.styleFailed an Alloy TSS style that is applied to the field if the validation failed. Use with $.createStyle().

You can validate one value independent from a field with validators.validateValue() like this:

var r = validators.validateValue(value, type, label, options)

Example:

var lastname = "Mueller";
var r = validators.validateValue(lastname, "minchar", "Lastname", {chars: 3, msg: 'At least three chars, please', allowAlert: true);
if (r != true) {
// do something when the validation failed
}