How to make macro mandatory/required using js?

Janna Pierre
Giga Contributor

I'm having trouble with scripting in the Macro I'm working with. I would like to make a field mandatory/required. The user must fill out the field before the form can be submitted. In the macro there HTML that defines the fields and input fields. Here is what I coded, but the script is not taking effect, I am able to submit when the field is blank (which is what I'm trying to fix)

 

<script>
         const input = document.querySelectorAll("#accprefix, #accName").value;
         input.setAttribute("required", "");
        
         document.getElementsByClass("accountName").onSubmit = function() {
            if(document.getElementById("accprefix").value = "" || document.getElementById("accName").value = "" )
            {
                alert("Account Name is required for submission");
            }
        };
        
    </script>

 

5 REPLIES 5

Yousaf
Giga Sage

Hi Janna,

May I ask the reason why are you not doing it with onSubmit client script?


***Mark Correct or Helpful if it helps.***

Please refer if this could help its not the same requirment but has a discussion related to the same topic.

Hiding a UI Macro based on Field Data


***Mark Correct or Helpful if it helps.***

The form I am working with has 2 forms in 1. One form uses account name the other has subscription name. They work the same using input values. When I used the client script I ran into the issue when I wanted to submit information in the subscription name, the alert for account name would display.

I created this Client Script (same for account name), both are currently inactive: 

function onSubmit() {
    //detects input in account name
    var subscription_name = g_form.getValue('subscription_name');
    var isAzure = g_form.getOption('cloud_provider').value;

    // if the subscription name is blank, the alert will appear and form should not submit
    if (g_form.isNewRecord() && subscription_name == '') {
        alert("A subscription name is required prior to submitting the form");
        g_form.setDisplay('subscription_name', true);
        g_form.setMandatory('subscription_name', true);
        return false;
    }
}

That's when I figured I probably needed to make the changes in the macro

Ok. got your point 
Need to check and make sure before answering.


***Mark Correct or Helpful if it helps.***