Question

I have a select list when i select the 'phone number' option i set regExp option in the number field so the user must enter a phone number in the required format. I also have an 'email' option in he select list and when selected i want to set the data-dojo-props for the number field.

In other words i am using one validationTextField to caputre both phone numbers and email address. However when the regExp is set it does not take the required effect and when the data-dojo-props is set for email it does not take the required effect. In both options if a valid value is entered or an invalid value is entered the validator does however show an error (red exclamation mark on field).

Code

<input type="text" required="true" name="contactString"
id="contactStringId" placeholder="Your Contact Number"
data-dojo-type="dijit/form/ValidationTextBox"                   
missingMessage="Please Enter Your Contact Number"
title="Contact Number / Email Address :" style="width: 25em;"/>  
 if (contactType.match(/^email|e-mail|mail.*$/)){
        dijit.byId('contactStringId').attr('invalidMessage', 'Invalid ' + contactType.toProperCase() +' Format: www.local-part@domain.com');                    
        dijit.byId('contactStringId').set('data-dojo-props', 'validator:dojox/validate/isEmailAddress');            
        console.log('email settings set');

    }else if(contactType.match(/^cell|home|work|fax|office.*$/)){
        dijit.byId('contactStringId').attr('invalidMessage', 'Invalid ' + contactType.toProperCase() +' Format: (xxx)-xxx-xxxx');   
        dijit.byId('contactStringId').set('regExp', '^[0-9]\d{2}-\d{3}-\d{4}$');        
        console.log('phone number settings set');           
    }
Was it helpful?

Solution

When you're setting dojo widget properties programmatically, you don't set data-dojo-props, you normally set the actual attribute you want to set. So you would do:

dijit.byId('contactStringId').set("validator", /*whatever validator you want here*/);

A better way to do this is simply by doing this:

var contactStringBox = dijit.byId('contactStringId');
contactStringBox.validator = function() {
  /*
   *whatever validation code you want here
   * like if(this.value === "test") { return false;}
   */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top