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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Validations in Portal page- My code not working properly

(0) ShareShare
ReportReport
Posted on by 467

Hi All,

I am using PowerApps portals in D365.

I have two radio buttons on the page(Yes and No) and my all controls are in section and I am hiding section when i select "No" radio button and showing when i select "Yes"

I want validations when select Yes radio button and don't want any validations if I select No radio button

I have implemented following(changed id's to not disclose source code) code. I am getting following problems

1. It is raising validations even I select "No" radio button and submit the form

2. It is adding duplicate validation error messages(It is happening when I keep on clicking on Yes and No radio buttons) and I submit the form

Please help me. It is bit urgent for me.

Here is my code : 

$(document).ready(function() {

if (($('#Radio_Yes').is(':checked')) && (!$('#Radio_No').is(':checked')))
{
addValidator('firstname', "Firstname");
addValidator('lastname', "Lastname");
}


function addValidator(fieldName, fieldLabel) {

if (typeof (Page_Validators) == 'undefined') return;
$("#" + fieldName + "_label").parent().addClass("required");
var newValidator = document.createElement('span');
newValidator.style.display = "none";
newValidator.controltovalidate = fieldName;
newValidator.errormessage = "<a href='#" + fieldName + "_label'>" + fieldLabel + " is a required field.</a>";
newValidator.validationGroup = "";
newValidator.initialvalue = "";
newValidator.evaluationfunction = function () {
var value = $("#" + fieldName).val();
if (value === null || value === "") {
return false;
} else {
return true;
}
};

function removeValidator(fieldName)
{
$.each(Page_Validators, function (index, validator) {
if (validator.id == "RequiredFieldValidator" + fieldName) {
Page_Validators.splice(index, 1);
}
});
$("#" + fieldName + "_label").parent().removeClass("required");

}

// Add the new validator to the page validators array:
if ($('#Radio_Yes').is(':checked'))
{
Page_Validators.push(newValidator);
}

$("#Radio_No").change(function() {
if (this.checked)
{

removeValidator("firstname");
removeValidator("lastname");
$("[data-name=Contact_Details]").parent().hide();
}
});

$("#Radio_Yes").change(function() {
if (this.checked)
{

addValidator('firstname', "Firstname");
addValidator('lastname', "Lastname");
$("[data-name=Contact_Details]").parent().show();

}
});

// Wire-up the click event handler of the validation summary link

$("a[href='#" + fieldName + "_label']").on("click", function () {
scrollToAndFocus(fieldName + '_label', fieldName);

});
}

});

Thanks in Advance

Babu

I have the same question (0)
  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi Babu,

    Missing "newValidator.id" and  you are trying to remove the "newValidator" object from Page_Validators by using id in "removeValidator" function. Please make following changes.

    newValidator.id = fieldName   "Validator";
    
    
    
    function removeValidator(fieldName) {
        Page_Validators = $.grep(Page_Validators, function (item) {
            return item.id !== fieldName   "Validator";
        });
    
        $("#"   fieldName   "_label").parent().removeClass("required");
    
    }
    
    
    // Add the new validator to the page validators array:
    if ($('#Radio_Yes').is(':checked') && Page_Validators.filter(e => e.id === fieldName   'Validator').length === 0) {
        Page_Validators.push(newValidator);
    }

    Thanks

    Justin Jose

  • Narendra Sunkara Profile Picture
    467 on at

    Hi Justin,

    Thanks for your response.

    I will try with suggested changes

    Regards,

    Babu

  • cloflyMao Profile Picture
    25,210 on at

    Hi Babu,

    It seems that there are several lines you should change to code:

    1. When you iterate Page_Validators array in removeValidator, 

    if (validator.id == "RequiredFieldValidator" fieldName)

    you should make id property of newValidator object also has same format, e.g:

    newValidator.id = "RequiredFieldValidator"  fieldname;

    2. There is some issue when I was debugging forEach function: cannot read undefined property id...

    I'm not so familiar about the function so I changed it like below and it worked then:

    for (var i = 0;i < Page_Validators.length; i  ) {
        if (Page_Validators[i].id == "RequiredFieldValidator"   fieldname) {
          Page_Validators.splice(i, 1);
        }
    }
    

    3. In newValidator.evaluationfunction snippet, we should still check fields value manually or statically, 

    because scope of the "evaluationfunction" is whole form.

    newValidator.evaluationfunction = function() {   
        if ($("#lastname").val() == "" || $("#firstname").val() == "") {
          return false;
        } else {
          return true;
        }       
    };

    My test code:

    $(document).ready(function() {
    
        function addValidator(fieldname, fieldLabel) {
          // Add event validator
          if (typeof(Page_Validators) == 'undefined') return;
        // Create new validator
          var newValidator = document.createElement('span');
          newValidator.style.display = "none";
          newValidator.id = fieldname   "Validator";
          newValidator.controltovalidate = fieldname;
          newValidator.errormessage = ""   fieldLabel   " is a required field.";
          // value = $("#"   fieldname).val();
          newValidator.evaluationfunction = function() {   
            debugger;            
            if ($("#emailaddress1").val() == "" || $("#firstname").val() == "") {
              return false;
            } else {
              return true;
            }       
          };
          Page_Validators.push(newValidator);
        }
    
        function removeValidator(fieldname) {
          debugger;
          for (var i = 0;i < Page_Validators.length; i  ) {
            if (Page_Validators[i].id == fieldname   "Validator") {
              Page_Validators.splice(i, 1);
            }
          }
          // $.each(Page_Validators, function (index, validator) {
          //   if (typeof validator.id !== "undefined" && validator.id == fieldname   "Validator") {
          //     Page_Validators.splice(index, 1);
          //   }
          // });
        }
    
        // Add validator element
        $("#new_acceptance_0").change(function() {
            if (this.checked) {
                removeValidator("emailaddress1");
                removeValidator("firstname");
                $("#emailaddress1").attr('aria-required', "false");
                $('#emailaddress1').parent().prev().removeClass("required");
                $("#firstname").attr('aria-required', "false");
                $('#firstname').parent().prev().removeClass("required");
            }
        });
    
        $("#new_acceptance_1").change(function() {
            if (this.checked) {
                addValidator("emailaddress1", "Email");
                addValidator("firstname", "First Name");
                $("#emailaddress1").attr('aria-required', "true");
                $('#emailaddress1').parent().prev().addClass("required");
                $("#firstname").attr('aria-required', "true");
                $('#firstname').parent().prev().addClass("required");
            }
        });
    
    })

    Form:

    pastedimage1586756028784v1.png

    Regards,

    Clofly

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans