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,...
Unanswered

Multiple Validation message showing in Portal

(0) ShareShare
ReportReport
Posted on by 437

Hi All,

In a page of CRM portal I am calling a function AddValidatior() in on page load and on some condition on chnage of a field1 i am calling the same function.

But if field1 has been change for two times and if this field in blank then 2 times multiple validation will display.

I think AddValidator is called 2 times so in Page_validator  this field get add multiple times. Can we restrict to add duplicate value in  page_validator  array so that if it gets call multiple times then also it will not add duplicate field in this array.

Or pls suggest any other way.

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 mandatory field.</a>";
newValidator.validationGroup = "";
newValidator.initialvalue = "";
newValidator.evaluationfunction = function () {
var value = $("#" + fieldName).val();
if (value === null || value === "") {
return false;
} else {
return true;
}
};

// Add the new validator to the page validators array:
Page_Validators.push(newValidator);

// Wire-up the click event handler of the validation summary link
$("a[href='#" + fieldName + "_label']").on("click", function () {
scrollToAndFocus(fieldName + '_label', fieldName);
});
}

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi windy,

    1. If field1 has been change for 3/4 times and if this field in blank, will then 3/4 times multiple validation will display?

    2. It seems that your validator was a span element; if so, you could directly remove newValidator element before next AddValidator is called.

    (By giving your span validation an ID or class name attribute, then remove it with the attribute name)

    3. If I didn't understand your description correctly, could you kindly share me reason for why would you like to create an array to save validator and a screenshot of your page?

    I thought that because your AddValidator would be called multiple times, so duplicate values will be added.

    We couldn't prevent a value from being added into an array without iteration check on the array, so you could instead do remove duplicate value job for your array before display validation element.

    https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array

    Or adding extra if condition check:

    such as only push newValidator into the array if length of Page_Validators equals 0.

    In conclusion, I thought that point 2 would be the option.

    For example, in a lead entity form,

    I'll always remove custom validator when job title hasn't value, but the validator will only display once:

    pastedimage1577690220916v1.png

      var jobField = $('#jobtitle');
      jobField.on('focusout', function() {
        $('.custom-notification').remove();
         if ($(this).val() == null || $(this).val() == "")
         $(this).parent().append('Please input your job title!');
      })

    Regards,

    Clofly

  • windyMill Profile Picture
    437 on at

    Hi Clofy,
    Thanks for your sugestion 

    1. If field1 has been change for 3/4 times and if this field in blank, will then 3/4 times multiple validation will display?  --YES

    2. It seems that your validator was a span element; if so, you could directly remove newValidator element before next AddValidator is called. I dindnt get this Can you please explain from code.

    (By giving your span validation an ID or class name attribute, then remove it with the attribute name)

    3. If I didn't understand your description correctly, could you kindly share me reason for why would you like to create an array to save validator and a screenshot of your page?  -  Let me explain this you 
    In portal suppoes I have 8 Fields. In that one  say F1 will be mandatory if  value  in  drop down field F2 select a particular elment E1. F1 will not be mandatory if user selected any element other than E1.

    Also on selection of Element  "E6" of Field 6 drop down   E7 should be mandatory.
    On selection of Element  "E7" of Field 7 drop down   E8 should be mandatory.

    Onpageload {

    }

    OnchangeofdropdownfiledF2{

     if (selectedElement == E1)

    {

     Addvalidator (Field F1)

    }

    Else

    {

    Make PageValidator.length =0
    AddValidator(Fileld2)

    AddValidator(Fileld3)
    AddValidator(Fileld4)
    AddValidator(Fileld5)
    }

    }

    OnChangeofField E6{

    Addvalidator(Field7)

    }

    OnChangeofField E7{

    Addvalidator(Field8)

    }

    The first selection of any fields works fine. But second time of seldction on field 6 other than E1 element shows duplicate error msg.
    Same happens on second time  selection  of Fileld 6 and 7

    Actually i have 30 fields but i explained the same scnario with 8 fields.

     

     

  • cloflyMao Profile Picture
    25,210 on at

    Hi Windy,

    Thanks for detailed explanation.

    -- It seems that your validator was a span element; if so, you could directly remove newValidator element before next AddValidator is called. I dindnt get this Can you please explain from code.

    That's my own custom validator, you could check code in my first response.

    Below is my summary on your business behavior:

    dependency 1: F2(E1) -> F1 will be mandatory

    dependency 2: F6(E6) -> F7 will be mandatory

    dependency 3: F7(E7) -> F8 will be mandatory

    Could I say that no matter how many dependencies are, the issue is only caused by Addvalidator function itself?

    If my thought would be right, could you share me your validator code for test?(just for one field is ok)

    Because I want to to get details about how did you listen your field value change event,

    for my own, I listen it by onfocusout.

    Regards,

    Clofly

     

  • windyMill Profile Picture
    437 on at

    Hi Clofy,

    Thanks for your valuable comments.

    My validation code is 

    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 mandatory field.</a>";
    newValidator.validationGroup = "";
    newValidator.initialvalue = "";
    newValidator.evaluationfunction = function () {
    var value = $("#" + fieldName).val();
    if (value === null || value === "") {
    return false;
    } else {
    return true;
    }
    };

    // Add the new validator to the page validators array:
    Page_Validators.push(newValidator);

    // Wire-up the click event handler of the validation summary link
    $("a[href='#" + fieldName + "_label']").on("click", function () {
    scrollToAndFocus(fieldName + '_label', fieldName);
    });
    }

    Code to remove validation 

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

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

    Below is code that  that how I am calling it 

    $(document).ready(function () {

      addValidator(fieldName1, fieldLabel1); 
     addValidator(fieldName2, fieldLabel2); 
     addValidator(fieldName3, fieldLabel3); 


    }

    $("#Filed1.change(function () {
    if (Page_Validators != null) {
    Page_Validators.length = 0;
    }

    var selectedValue = $("#Filed1).val();
    if (selectedValue != "66dff-4e04-ea11-a811-676723") { //non std, show and make mandatory
    showSection("Section_1");
    showSection("Section_2_0");



    addValidator("fieldd2", "1.x");
    addValidator("fieldd3", "1.3");
    addValidator("fieldd4", "2.1");

    addValidator("fieldd4", "xxx4");
    addValidator("fieldd5", "xxx5");

    }
    else if (selectedValue == "e8712d864e-4e04-ea11-a811-66aasty") {

    addValidator("fieldd2", "1.x");
    addValidator("fieldd3", "1.3");
    addValidator("fieldd4", "2.1");

    addValidator("fieldd4", "xxx4");
    addValidator("fieldd5", "xxx5");

    }

    }

    Regards,
    Windy

  • cloflyMao Profile Picture
    25,210 on at

    Hi Windy,

    I've applied addValidator() to my business phone field,

    it'll add a red requiring label to the field:

    pastedimage1577873822562v1.png

    And validator will only appear once regardless of how many time the submit button is clicked.

    pastedimage1577873935390v2.png

    Full code:

    $(document).ready(function() {
    
        addValidator('telephone1', "12.x");
      
        function addValidator(fieldName, fieldLabel) {
          
          if (typeof (Page_Validators) == 'undefined') return;
          $("#"   fieldName   "_label").parent().addClass("required");
          debugger;
          var newValidator = document.createElement('span');
          newValidator.style.display = "none";
          newValidator.controltovalidate = fieldName;
          newValidator.errormessage = ""   fieldLabel   " is a mandatory field.";
          newValidator.validationGroup = "";
          newValidator.initialvalue = "";
          newValidator.evaluationfunction = function () {
              var value = $("#"   fieldName).val();
              if (value === null || value === "") {
                  return false;
              } else {
                  return true;
              }
          };
      
          // Add the new validator to the page validators array:
          Page_Validators.push(newValidator);
      
          // Wire-up the click event handler of the validation summary link
          $("a[href='#"   fieldName   "_label']").on("click", function () {
              scrollToAndFocus(fieldName   '_label', fieldName);
          });
      }
      
      });

  • Ashwin Sriramulu Profile Picture
    10 on at

    Try to add 'RemoveValidation' Method everytime before you call 'AddValidation'

    RemoveValidation("xxx");

    AddValidation("xxx");

    function RemoveValidation(field) {

       if (!field) return;

       var i;

       var hide;

       for (i = 0; i < Page_Validators.length; i++) {

           if (typeof (Page_Validators[i].id) != 'undefined') {

               if (Page_Validators[i].id == "Custom" + field + "Validator") {

                   Page_Validators[i].evaluationfunction = true;

                   hide = document.getElementById("CustomRequiredFieldValidator" + field);

                   $("#CustomRequiredFieldValidator" + field).hide();

               }

           }

       }

    }

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 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans