In free-form text fields it's sometimes possible to input a value that doesn't make sense. For example, when asking someone for their e-mail address it might be necessary to validate it against some kind of regular expression like .+@.+
to provide a simplistic test. Perform the following steps:
offset
which allows valid values between -14
and +12
. (By default, IntegerFieldEditor
validates against the range 0
to MAX_INT
.) Add the following to the createFieldEditors()
method:IntegerFieldEditor offset = new IntegerFieldEditor("offset","Current offset from GMT", getFieldEditorParent()); offset.setValidRange(-14, +12); addField(offset);
Each field editor can determine what is (or is not) valid, and the validity of the page as a whole is a conjunction of all of the individual field editors' validity. It's also possible to create custom validation rules by creating a subclass of the appropriate FieldEditor
by overriding the isValid()
method appropriately.
The error message will display the warning, "Value must be an Integer between -14 and 12" at the top of the preference page."