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

kirankawle
Tera Contributor

function validateMandatoryField() {
var field = document.getElementById('mandatoryField');
if (!field.value.trim()) {
alert('This field is mandatory. Please fill it out.');
return false; // Prevent form submission
}
return true; // Allow form submission
}

 

lets try this code 

@kirankawle

I have to write this in client script section of the UI pAge?

yes can you please try it on client script section of the UI page.it should work .

 

GopikaP
Mega Sage

Hi @SamiranD , you will need validation in client script to see if the field has value or not.

Can you share your HTML and Client Script? 

Please refer:

Solved: UI Page, Reference field mandatory not working - ServiceNow Community