Dynamic field + bootstrap validation

Avatar

Avatar
Coach
Level 6
David__Garcia
Level 6

Likes

107 likes

Total Posts

217 posts

Correct reply

45 solutions
Top badges earned
Coach
Builder
Engage 10
Seeker
Bedrock
View profile

Avatar
Coach
Level 6
David__Garcia
Level 6

Likes

107 likes

Total Posts

217 posts

Correct reply

45 solutions
Top badges earned
Coach
Builder
Engage 10
Seeker
Bedrock
View profile
David__Garcia
Level 6

03-03-2021

I am displaying/hiding form elements based on a user dropdown select option, the following script is meant to add/remove required attributes on the fields, however, it seems that validation is not taking this into account, could it be that validation script only runs on page load rather than on dynamic form changes? if so, how can I make it listen to changes in form required fields.

 

3Lz9B

 

function MyOnChange(invesType){
  var x = document.getElementById("iProfessional");
  if (invesType != "privateClients") {
    x.style.display = "block";
    $("#companyname").attr("data-nl-ismandatory","true");    
  $('#companyname').prop('required', true)
  
    $("#jobtitle").attr("data-nl-ismandatory","true");    
  $('#jobtitle').prop('required', true)
  
    
    } else {
    x.style.display = "none";
     $("#companyname").attr("data-nl-ismandatory","false");
    $("#companyname").removeAttr("required");
  
    $("#jobtitle").attr("data-nl-ismandatory","false");
    $("#jobtitle").removeAttr("required");  
    }    
  
}

 Upon submitting the form after the required and data-nl-ismandatory have been removed, Adobe Campaign is showing that these fields are mandatory.

david_garcia1_0-1614778471668.png

 

Accepted Solutions (0)

Answers (1)

Answers (1)

Avatar

Avatar
Affirm 100
MVP
_Manoj_Kumar
MVP

Likes

126 likes

Total Posts

329 posts

Correct reply

103 solutions
Top badges earned
Affirm 100
Validate 1
Contributor
Ignite 1
Shape 1
View profile

Avatar
Affirm 100
MVP
_Manoj_Kumar
MVP

Likes

126 likes

Total Posts

329 posts

Correct reply

103 solutions
Top badges earned
Affirm 100
Validate 1
Contributor
Ignite 1
Shape 1
View profile
_Manoj_Kumar
MVP

14-03-2021

Hello @David__Garcia 

 

Remove all "data-nl-mandatory" tags and validate everything with the javascript. nl tags won't consider the dynamic changes.

Remove the validation log from OnChange function and add the logic in the form Validation function. The form validation function will look like this

 function formValidation(){
	var jobTitle=$("#jobtitle").val();
	var companyname=$("#companyname").val();
	if(jobTitle!="privateClients"){
		if(companyname==''){		
			alert("Company Name is required"); // you may want show it in error div via innerHTML
			return false;
		}
	}
	document.controller.submit("NEXT_TRANSITION_NAME");	
 }

 

and the submit button will have this onclick attribute.

onclick="return formValidation()"

 

It should work in this case.