Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

Making a field in UI page mandatory

SamiranD
Tera Contributor

I want to make a field mandatory in an UI Page and by using required tag it is not working. Any workaround? maybe some javascript code or any other thing?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

@SamiranD 

1 way is to use client script in UI page

Other way is check how OOB UI pages from ServiceNow is being done, check that code.

refer these link if that helps

creating a mandatory field in ui page?

Make UI Page Text area mandatory

If my response helped please mark it correct and close the thread so that it benefits future readers.

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron

@SamiranD 

1 way is to use client script in UI page

Other way is check how OOB UI pages from ServiceNow is being done, check that code.

refer these link if that helps

creating a mandatory field in ui page?

Make UI Page Text area mandatory

If my response helped please mark it correct and close the thread so that it benefits future readers.

SamiranD_0-1742379596071.png

I have implemented the above logic. with the below html.

 

SamiranD_1-1742379780940.png

Please advise

@SamiranD 

both the links I shared has working approach

which one did you try and what's not working?

@GopikaP 

the 1st link worked for me, response from Rahul

HTML:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<style>
label.required::before {
  content: '*';
  margin-right: 4px;
  color: red;
}
label.notrequired::before {
  content: '*';
  margin-right: 4px;
  color: grey;
}
</style>

<label for="input1" id="inputLabel" >Name</label>
  <input type="text" ng-model="name" onchange="checkMandatory()" id="demo"  />

</j:jelly>

Client Script:

document.getElementById('inputLabel').setAttribute("class", "required");

function checkMandatory(){
if(document.getElementById('demo')!="")
document.getElementById('inputLabel').setAttribute("class", "notrequired");

}

Output

html mandatory.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

@SamiranD 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.