James Irwin
James Irwin

Reputation: 1191

Use dojox.validate on Dijit ValidationTextBox, but not make text required

In my Dijit form, I have the following ValidationTextBox:

<input data-dojo-type="dijit/form/ValidationTextBox"
    name="email" data-dojo-props="required: true,
    validator:dojox.validate.isEmailAddress, 
    invalidMessage: 'Invalid email'" />

However, I would like to make the field not required (i.e. allow it to be empty), but still use the dojox.validate method. Removing required: true or explicitly setting required: false doesn't seem to work; the field still says "Invalid email." How can I only use the validation method if text is present?

Upvotes: 0

Views: 1002

Answers (1)

Astra Bear
Astra Bear

Reputation: 2738

Write your own validator and set required=false, so something like:

 <input data-dojo-type="dijit/form/ValidationTextBox"
    name="email" data-dojo-props="required: false,
    validator: function(text){
                   if (!text){
                     return true;
                   }
                   return dojox.validate.isEmailAddress(text);
                  },
    invalidMessage: 'Invalid email'" />

Upvotes: 3

Related Questions