web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Make fields required/n...
Power Pages
Answered

Make fields required/not required based on field checkbox

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi! 

I read alot about the validation approach to set fields that are required to not required. 

Unfortunately I was not able to copy the validation code provided on the forum and adapt it to my requirement, thus I am seeking help.

 

 

I want to make the Account Lookup field (parentcustomerid) not-required as soon as the beneath checkbox "Ich finde meine Orgnisation nicht/I dont find my Organisation" (xxx_ichfindemeineorgnicht) is checked.

newmay_1-1631786091727.png

 

Could any of you assist in creating the validation code with these two field names (parentcustomerid & xxx_ichfindemeineorgnicht) pasted in, since I am doing something wrong.

 

 

 

Every guidance / assistance is much appreciated!

Categories:
  • H V Profile Picture
    1,510 on at

    Hi @Anonymous 

     

    Please add below code in Custom Javascript:

    • Go to your basic form
    • Click on Additional Settings tab
    • Add below code in Custom Javascript textbox
    • Clear the cache and test it

     

     

    $(document).ready(function() {
    
    	// Add event validator
    	if (typeof(Page_Validators) == 'undefined') return;
    		// Create new validator
    		var newValidator = document.createElement('span');
    		newValidator.style.display = "none";
    		newValidator.id = "parentcustomeridValidator";
    		newValidator.controltovalidate = "parentcustomerid";
    		newValidator.errormessage = "<a href='#parentcustomerid_label'>Organisation is a required field.</a>";
    		newValidator.evaluationfunction = function() { 
    		
    		if(!$("#xxx_ichfindemeineorgnicht").is(':checked'))
    		{
    			if ($('#parentcustomerid').val() == "") {
    				return false;
    			} else {
    				return true;
    			}
    		}
    		else
    		{
    			return true;
    		} 
    	};
    	Page_Validators.push(newValidator);
    
    	// Add validator element
    	$("#xxx_ichfindemeineorgnicht").change(function() {
    		if (this.checked) {
    			$("#parentcustomerid").attr('aria-required', "false");
    		$('#parentcustomerid').parent().parent().prev().removeClass("required");
    		}
    		else
    		{
    			$("#parentcustomerid").attr('aria-required', "true");
    		 $('#regardingobjectid').parent().parent().prev().addClass("required");
    		}
    	});
    
    })

     

     

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hi @hardikv 

     

    Thanks for the code! As soon as I check the box the Account Lookup is set to optional -thats how we wanted!

    However now we want to submit the form which is not possible 🙂 

     

    Because I think we defined an error message within the validation.

     

    So basically if a user does not find the org in the Account Lookup (parentcustomerid) he should check the checkbox (xxx_ichfindemeineorgnicht) which makes the Account Lookup (parentcustomerid) optional and we can submit the form without the organisation selected

  • H V Profile Picture
    1,510 on at

    Hi @Anonymous 

     

    May I know by default Account Lookup (parentcustomerid) is required from the form?

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @hardikv 

    Yes the parentcustomerid is a required field!

  • H V Profile Picture
    1,510 on at

    Hi @Anonymous 

     

    Okay, make it optional from the form(Customization) and add below code. Hope this code will fulfil your requirement.

     

     

    $(document).ready(function() {
    	
    	if($("#xxx_ichfindemeineorgnicht").is(':checked'))
    	{
    		$("#parentcustomerid").attr('aria-required', "false");
    		$('#parentcustomerid').parent().parent().prev().removeClass("required");
    	}
    	else{
    		$("#parentcustomerid").attr('aria-required', "true");
    		$('#regardingobjectid').parent().parent().prev().addClass("required");
    	}
    	
    	// Add event validator
    	if (typeof(Page_Validators) == 'undefined') return;
    		// Create new validator
    		var newValidator = document.createElement('span');
    		newValidator.style.display = "none";
    		newValidator.id = "parentcustomeridValidator";
    		newValidator.controltovalidate = "parentcustomerid";
    		newValidator.errormessage = "<a href='#parentcustomerid_label'>Organisation is a required field.</a>";
    		newValidator.evaluationfunction = function() { 
    		
    		if(!$("#xxx_ichfindemeineorgnicht").is(':checked'))
    		{
    			if ($('#parentcustomerid').val() == "") {
    				return false;
    			} else {
    				return true;
    			}
    		}
    		else
    		{
    			return true;
    		} 
    	};
    	Page_Validators.push(newValidator);
    
    	// Add validator element
    	$("#xxx_ichfindemeineorgnicht").change(function() {
    		if (this.checked) {
    			$("#parentcustomerid").attr('aria-required', "false");
    		$('#parentcustomerid').parent().parent().prev().removeClass("required");
    		}
    		else
    		{
    			$("#parentcustomerid").attr('aria-required', "true");
    		 $('#parentcustomerid').parent().parent().prev().addClass("required");
    		}
    	});
    
    })

     

     

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @hardikv ok yet the issue is that we need that standard field to be mandatory within our CRM we only need to set it optional for this use case?

  • Verified answer
    H V Profile Picture
    1,510 on at

    Hi @Anonymous 

     

    Please check with below code. It will fulfill to check either one of your field should be selected:

    $(document).ready(function() {
    	
    	removeValidator("parentcustomerid");
    	$("#parentcustomerid").parent().prev().removeClass('required');
    
    	// Add event validator
    	if (typeof(Page_Validators) == 'undefined') return;
    
    		// Create new validator
    		var newValidator = document.createElement('span');
    		newValidator.style.display = "none";
    		newValidator.id = "parentcustomeridValidator";
    		newValidator.controltovalidate = "parentcustomerid";
    		newValidator.errormessage = "<a href='#parentcustomerid_label'>Select parentcustomerid or xxx_ichfindemeineorgnicht field</a>";
    		newValidator.evaluationfunction = function() { 
    
    		if($("#xxx_ichfindemeineorgnicht").is(':checked'))
    		{
    			return true;
    		}
    		else
    		{
    			if ($('#parentcustomerid').val() == "") {
    				return false;
    			} else {
    				return true;
    			}
    		} 
    	};
    	Page_Validators.push(newValidator);
    })
    
    function removeValidator(fieldName) {
     var count = 0;
     for(var i =0; i < Page_Validators.length - count; i++){
     if (Page_Validators[i - count].id == "RequiredFieldValidator" + fieldName) {
     Page_Validators.splice(i - count, 1);
     count++;
     }
     }
     $("#" + fieldName + "_label").parent().removeClass("required");
    }

     

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @hardikv Thank you very much it worked!! Is it possible to switch the red Asterix by any chance? So the user first recognizes the asterix over the account lookup (signalises him/her the field is mandatory) - as soon as he checks xxx_ichfindemeineorgnicht the asterix disappears.

     

    Can this be done?

  • Verified answer
    H V Profile Picture
    1,510 on at

    Hi @Anonymous 

     

    Please try below code to achieve your latest requirement:

    $(document).ready(function() {
    	
    	removeValidator("parentcustomerid");
    	//$("#parentcustomerid").parent().prev().removeClass('required');
    
    	// Add event validator
    	if (typeof(Page_Validators) == 'undefined') return;
    
    		// Create new validator
    		var newValidator = document.createElement('span');
    		newValidator.style.display = "none";
    		newValidator.id = "parentcustomeridValidator";
    		newValidator.controltovalidate = "parentcustomerid";
    		newValidator.errormessage = "<a href='#parentcustomerid_label'>Select parentcustomerid or xxx_ichfindemeineorgnicht field</a>";
    		newValidator.evaluationfunction = function() { 
    
    		if($("#xxx_ichfindemeineorgnicht").is(':checked'))
    		{
    			return true;
    		}
    		else
    		{
    			if ($('#parentcustomerid').val() == "") {
    				return false;
    			} else {
    				return true;
    			}
    		} 
    	};
    	Page_Validators.push(newValidator);
    	
    	$("#xxx_ichfindemeineorgnicht").change(function() {
    		if (this.checked) {
    			$("#parentcustomerid").attr('aria-required', "false");
    			$('#parentcustomerid').parent().parent().prev().removeClass("required");
    		}
    		else
    		{
    			$("#parentcustomerid").attr('aria-required', "true");
    			$('#parentcustomerid').parent().parent().prev().addClass("required");
    		}
    	});
    })
    
    function removeValidator(fieldName) {
     var count = 0;
     for(var i =0; i < Page_Validators.length - count; i++){
     if (Page_Validators[i - count].id == "RequiredFieldValidator" + fieldName) {
     Page_Validators.splice(i - count, 1);
     count++;
     }
     }
     //$("#" + fieldName + "_label").parent().removeClass("required");
    }

    --------------------------
    If you like this post, give a Thumbs up. Where it solved your query, Mark as a Solution so it can help other people!

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanks for the fast reply @hardikv ! And of course for the guidance and help! 

     

    THANK YOU!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Pages

#1
M Bilal Khan Profile Picture

M Bilal Khan 99

#2
Mohsin Ali Profile Picture

Mohsin Ali 37

#3
11manish Profile Picture

11manish 21 Super User 2026 Season 2

Last 30 days Overall leaderboard