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 / Power Pages Custom Jav...
Power Pages
Unanswered

Power Pages Custom Javascript Multiple Push Validators

(0) ShareShare
ReportReport
Posted on by 4

Hi I have similar issue.

 

My Scenario is I have a preferred contact type option set and when user chooses email I hide contact number and show email value and on submit if it is missing proper value I have to fire error and not allow form submission and vice versa for when user choses phone number.

 

The code runs fine if chose 1 but if I switch to other  it wont fire and finishes form submission. Please review and let me know where should I correct my code.

if (window.jQuery) {
(function ($) {
$(document).ready(function () {
showHideControls();
$("#dhhs_emailaddress").closest("td").find("div.control, div.info").hide();
$("#dhhs_contactnumber").closest("td").find("div.control, div.info").hide();
$('#dhhs_preferredmethodofcontact').change(function () {
if ($('#dhhs_preferredmethodofcontact').length) {
showHideControls();
}
});
$('#dhhs_contactnumber').change(function () {
formatPhoneNumber(this);
});
});
}(window.jQuery));
}
//Format Phone NUmber
function formatPhoneNumber(phoneNumberField) {
var username_length = $(phoneNumberField).val().length;
if (username_length == 10) {
$(phoneNumberField).val($(phoneNumberField).val().replace(/^(\d{3})(\d{3})(\d+)$/, "$1-$2-$3"));
}
}

//Pass Id to makeRequired & makeNotRequired function
function showHideControls() {
debugger;

var preferredMethodOfContact = $("#dhhs_preferredmethodofcontact").val();
if (preferredMethodOfContact == 372950000) //if it’s a Email
{
MakeFieldRequired('dhhs_emailaddress', 'is a required field.');
makeNotRequired('dhhs_contactnumber');
ShowHideRequired('dhhs_emailaddress', 'dhhs_contactnumber');
}
else if (preferredMethodOfContact == 372950001) //if it is Phone
{
MakeFieldRequired('dhhs_contactnumber', 'has to be US 10 digit format and is a required field.');
makeNotRequired('dhhs_emailaddress');
ShowHideRequired('dhhs_contactnumber', 'dhhs_emailaddress');
}
}

//Make field required.
function MakeFieldRequired(fieldName, errorMessage) {
try {

if ($("#" + fieldName) != undefined) {
$("#" + fieldName).prop('required', true);
$("#" + fieldName).closest(".control").prev().addClass("required");
// Create new validator
var Requiredvalidator = document.createElement('span');
Requiredvalidator.style.display = "none";
Requiredvalidator.id = fieldName + "Validator";
Requiredvalidator.controltovalidate = fieldName;
Requiredvalidator.errormessage = "<a href='#" + fieldName + "_label'>" + $("#" + fieldName + "_label").html() + errorMessage + "</a>";
Requiredvalidator.initialvalue = "";
Requiredvalidator.evaluationfunction = function () {
var value = $("#" + fieldName).val();
alert('value of email ' + value);
if (value == null || value == "") {
return false;
}
else if (value < 10 || value > 10) {
if (fieldName == 'dhhs_contactnumber') {
return false;
}
}
return true;
};

// Add the new validator to the page validators array:
Page_Validators.push(Requiredvalidator);
}
}
catch (error) {
errorHandler(error);
}
}
function makeNotRequired(fieldName) {
//Clean up previous validator <span> if present
if ($('#ValidationSummaryEntityFormView').length) {
$('#ValidationSummaryEntityFormView').hide();
}

try {
if ($("#" + fieldName) != undefined) {
$("#" + fieldName).closest(".control").prev().removeClass("required");
$("#" + fieldName).prop('required', false);

for (i = 0; i < Page_Validators.length; i++) {
if (Page_Validators[i
].id == fieldName + "Validator") {
Page_Validators.splice(i);
}
}
}
}
catch (error) {
errorHandler(error);
}
}

function ShowHideRequired(fieldNameToShow, fieldNameToHide) {
try {
if ($("#" + fieldNameToShow) != undefined)
$("#" + fieldNameToShow).closest("td").find("div.control, div.info").show();
if ($("#" + fieldNameToHide) != undefined)
$("#" + fieldNameToHide).closest("td").find("div.control, div.info").hide();
}
catch (error) {
errorHandler(error);
}
}// JavaScript source code

Categories:
I have the same question (0)
  • Lucas001 Profile Picture
    2,557 Super User 2026 Season 1 on at

    Hi @LS101,

     

    are both fields set as mandatory in your form? I don't think so as the form could not be submitted one or the other way depending on the choice of your user.

     

    An easier approach would be on my opinion to disable the button as long as your requirements are not met.

    You can use the change event as you already have done it, look which fields are shown and if those are filled. If not display an error and keep the button disabled.

     

    When posting code use the code function in your post for readability:

    Lucas001_0-1690523177737.png


    --------------------------------------------------------------------------------------

    Hope that helps.

    If the post solves your problem, please mark it as solution. If it helps, consider a thumbs up.

  • Fubar Profile Picture
    8,489 Super User 2026 Season 1 on at

    May not be all the issues but you have some of your script split over multiple lines - which will error

    if (Page_Validators[i
    ].id == fieldName + "Validator") {
    Page_Validators.splice(i);
    }

    Should be

    if (Page_Validators[i].id == fieldName + "Validator") {
     Page_Validators.splice(i);
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
11manish Profile Picture

11manish 52

#2
Valantis Profile Picture

Valantis 46

#3
Vish WR Profile Picture

Vish WR 29

Last 30 days Overall leaderboard