How to: Making fields valid or invalid via custom JS

Created by Joshua Miller, Modified on Mon, 24 Mar at 10:27 PM by Joshua Miller

A field can be marked valid or invalid via Custom JavaScript. Setting a field invalid will prevent the form from being submitted. An invalid message can be setup to show below the field when the field is marked invalid.
(Note: A field has to be dirty for the validation message to show.)

Instructions

  1. Visit the Field List within the module for which the form field is to be made valid/invalid.

  2. To setup a validation message, the following JavaScript must be executed in the form field on Init/Blur.
    fieldScope.addValidationMessage("MyValidationName", "My test validation");

  3. The method 'addValidationMessage' on the fieldScope takes a key for the validation and a corresponding message.

  4. Review the form to ensure the Message and field reflect the validation applied through the Custom JavaScript.

 

Tip: It’s useful to use these within Conditional Statements to apply Valid/Invalid message accordingly.

 

Marking a field as invalid/valid

To mark a field as valid / invalid use the following JavaScript code.

 //to set the field to be invalid.

fieldScope.setValidity('MyValidationName', false)

 //to set the field to be valid.
fieldScope.setValidity('MyValidationName', true)

 

Marking another field on the form as invalid/valid

To mark a field other than the one referenced by the fieldScope, use the following JavaScript code.

//to mark another field invalid.

$scope.setFieldValidity(<display name of the other field>, 'MyValidationName', false); 

//to mark another field valid.

$scope.setFieldValidity(<display name of the other field>, 'MyValidationName', true); 

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article