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);
});
}
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();
}
}
}
}
Hi Windy,
I've applied addValidator() to my business phone field,
it'll add a red requiring label to the field:
And validator will only appear once regardless of how many time the submit button is clicked.
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); }); } });
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
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
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.
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:
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
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156