Save button on SharePoint form should validate custom or conditionally mandate field.

VG 0 Reputation points
2024-07-23T08:44:50.61+00:00

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
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.