Skip to content

Instantly share code, notes, and snippets.

@djeet
Created June 15, 2016 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djeet/efbd4e6a6e875f12b192035d1a8f6797 to your computer and use it in GitHub Desktop.
Save djeet/efbd4e6a6e875f12b192035d1a8f6797 to your computer and use it in GitHub Desktop.
Envoke Real Time Validation to Trigger Custom Message
/**
* Configure message and ID
*/
var error_message_custom = "___YOUR MESSAGE_HERE_____"; //Your custom error message
var current_field_id = 1; // Your Field ID
/**
* Script to envoke Real Time Validation to maintain error state
*/
all_validations[current_field_id].message = error_message_custom;
all_validations[current_field_id].validationFailed = true;
all_validations[current_field_id].displayMessageWhenEmpty = true;
all_validations[current_field_id].insertMessage(all_validations[current_field_id].createMessageSpan());
all_validations[current_field_id].addFieldClass();
all_validations[current_field_id].afterInvalid(all_validations[current_field_id].element);
@mastef
Copy link

mastef commented Oct 30, 2018

@djeet thanks for this example.

  1. Would be great if you could update this example with the latest function, as you'll need the following changes :
    all_validations[current_field_id] -> all_validations[current_form_id][current_field_id]
  2. Also how to clear the error message after the user entered a valid input? I tried with the following code :
all_validations[current_form_id][current_field_id].message = original_message_text; // ?
all_validations[current_form_id][current_field_id].validationFailed = false;
all_validations[current_form_id][current_field_id].displayMessageWhenEmpty = false;
all_validations[current_form_id][current_field_id].insertMessage(all_validations[current_form_id][current_field_id].createMessageSpan());
all_validations[current_form_id][current_field_id].addFieldClass();
all_validations[current_form_id][current_field_id].afterValid(all_validations[current_form_id][current_field_id].element);
  1. How to reset the error message, as it doesn't seem to get picked up
  2. How to wait for the existing validation code of the field to run, before triggering our own validation code? Basically I'd like to wait for all_validations[1][12].validationFailed to be filled out, before attempting a check ( or not )
    You could have a hook inside afterInvalid() and afterValid() - something like this
if(this.hasOwnProperty('afterCheckCallback') && !!this.afterCheckCallback && typeof this.afterCheckCallback === "function") {
    this.afterCheckCallback();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment