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

Notifications

Announcements

Community site session details

Community site session details

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

Make fields required/not required based on field checkbox

(0) ShareShare
ReportReport
Posted on by

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:
I have the same question (0)
  • 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
    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
    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
    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
    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
    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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Jerry-IN Profile Picture

Jerry-IN 71

#2
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard