The best practice method to provide separate validation rules for a particular widget is overriding the default validator()
method of that widget.
var fnameTextBox = dijit.byId('fnameTextBox');
fnameTextBox.validator = function() {
if(this.value.indexOf("John")>-1) {
this.set("invalidMessage",this.value+" is not allowed!");
return false;
}
return true;
}
//below 2 lines to programmatically validate a specific field
fnameTextBox._hasBeenBlurred = true;
fnameTextBox.validate();
//To show the message on failing the field must be focussed
fnameTextBox.focus();
The validator function can have its own logic for validating and is subjective only to this field widget. If same has to be applied to more than one, define the function instead of anonymous, and use it for the respective fields.
But, if it only a value check, we can give just a regexp expression for this.
regExp: '^((?!John).)*$'
The following is not the best practice to do it. Because calling form.validate() will still validate this field as true, which is not correct.
dijit.byId("fnameTextBox")._set("state","Error");
dijit.byId("fnameTextBox").attr('_ErrorMessage', 'John is not allowed');