Save button on SharePoint form should validate custom or conditionally mandate field.
VG
0
Reputation points
Hi All,
I'm working on SharePoint form in which I have some mandatory fields(made through default content type) and one conditionally mandate field called 'Name'. When I click save on the form its showing 'This setting must not be empty' for default mandate fields. I have written below code in Script editor web part to show 'Name' as mandatory when its visible on the form under certain condition, but form is getting saved even this field is blank. Can someone please help on this requirement?
<script>
function PreSaveAction()
{ var customValid = validateNameField();
console.log('customValid ', customValid);
return customValid;
}
$("input[value='Save']").click(function(e){
console.log('Clicked Save..' , SPClientForms.ClientFormManager.SubmitClientForm('aspnetForm'));
if(PreSaveAction())
{ console.log('PreSaveAction is true'); }
else
{ console.log('preventing save..! ');
e.preventDefault();
e.stopPropagation(); }
});
function validateNameField()
{
var nameValue = $("input[title='Name']").val();
if(nameValue === "")
{ isValid = false;
console.log('Name field is empty');
$("input[title='Name']").prop('required', true);
} }
else{ isValid = true; }
return isValid;
</script>
Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | Development
The process of building custom applications and tools that interact with Microsoft SharePoint, including SharePoint Online in Microsoft 365.
Sign in to answer