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