Karthik Ramachandran
Karthik Ramachandran

Reputation: 12175

check to see if dijit.form.ValidationTextBox has foucs in a validator

I want to write a custom valadiator for a dijit.form.ValidationTextBox. Unfortunately, dijit.form.ValidationTextBox.validator is called each type an ontype event occurs. To get around this the documentation suggests :

There is one small catch here: this validator will be called onType, meaning it will be sending requests to the backend on every key stroke. If you do not want that to happen, you may want to add another check in the beginning so that it always returns true if the validation text box is on focus

However, the don't quite mention how to check and see if the textbox has focus.

Any suggestion?

Upvotes: 0

Views: 1976

Answers (1)

Sean McIntyre
Sean McIntyre

Reputation: 23

In the documentation you mentioned (found here http://dojotoolkit.org/reference-guide/dijit/form/ValidationTextBox-tricks.html#dijit-form-validationtextbox-tricks) they mention creating a custom validation function. Try adding the following check to the beginning of the function:

dijit.byId("validationTextBoxNodeId").validator = function (value, constraints) {
    if(document.activeElement.id == "validationTextBoxNodeId") return true; //don't check while typing
    // Check that email has not been used yet.
    if (some-checks) {
        return true;
    } else {
        return false;
    }
}

Or, if you're not assigning a manual id such as "validationTextBoxNodeId" to the validation text box and are creating everything programmatically (which has, in my experience, been the far more likely scenario):

new dijit.form.ValidationTextBox({
    name:"email",
    //insert other properties like value, required or invalidMessage here
    validator: function() {
        if(this.id == document.activeElement.id) return true; //don't check while typing
        //real checks go here
        if (some-checks) {
            return true;
        } else {
            return false;
        }
    }
});

Notice you don't need to explicitly refer to the id of the box because the validation function works within its scope. You could probably accomplish the same thing in the first example using dojo.hitch() if you needed to.

Also, note that this only works if your target browser(s) supports document.activeElement ( Which browsers support document.activeElement?).

Upvotes: 2

Related Questions