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
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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader