Sharepoint Pre Save Lookup Validation
-
08-02-2021 - |
Pregunta
I'm new to SharePoint, so will appreciate any help here.
I'm trying to run two checks before a user can submit a form, be it on a new or edit form. The first check is the value of a field is >0 and if yes, check for an attachment else trigger an error message. This one I've been able to figure out, and I've included the code below.
I need to also Check if the value in a lookup drop down column is 000 (default value), and trigger an error message if it is, so I can force the user to update the column. JavaScript that would normally work on a regular drop-down field does not seem to work here.
Code 1: Checks if a field is greater than 0 and if yes, an attachment is required.
function PreSaveAction() {
var SuspiciousActivityAmountValue= document.getElementById("Suspicious_x0020_Activity_x0020__2395d776-7e2a-4c21-b298-86e337a4780d_$CurrencyField").value;
var atch = document.getElementById("idAttachmentsTable");
if(SuspiciousActivityAmountValue > "0"){
if (atch == null || atch.rows.length == 0)
{
alert("The Case Summary file appears to be missing! Please attach the document before saving the Security Notice.");
return false ;
}else {
return true;
}
}else{
return true;
}
}
Code 2: Check if the value in a lookup drop down column is 000 (default value), and trigger an error message if it is. Here's the script I was trying to use to get the second check to work.
function PreSaveAction() {
var BranchValue= document.getElementById("Branch_e29ef8cb-6b73-4418-83e1-6bb5cdbdba25_$LookupField").value;
if(BranchValue == "000"){
alert("Branch cannot be 000. Please select the appropriate branch number from the list");
return false ;
}else {
return true;
}
}
Appreciate any help I can get.
Edit: Here's what i tried by merging both codes, but the second check does not occur though the first one does. What do you think I'm doing wrong?
function PreSaveAction() {
var SuspiciousActivityAmountValue= document.getElementById("Suspicious_x0020_Activity_x0020__2395d776-7e2a-4c21-b298-86e337a4780d_$CurrencyField").value;
var atch = document.getElementById("idAttachmentsTable");
if(SuspiciousActivityAmountValue > "0"){
if (atch == null || atch.rows.length == 0)
{
alert("The Case Summary file appears to be missing! Please attach the document before saving the Security Notice.");
return false ;
}else {
return true;
}
}else{
return true;
var BranchDropDown= document.getElementById("Branch_de9394fa-0532-4926-b180-6ffe6d4703a8_$LookupField");
var BranchValue =BranchDropDown.options[BranchDropDown.selectedIndex].text if(BranchValue == "000"){
alert("Branch cannot be 000. Please select the appropriate branch number from the list");
return false ;
}else {
return true;
}
}
}
Solución
For validation to the lookup dropdown selected text, please modify the Script as below:
function PreSaveAction()
{
var BranchDropDown= document.getElementById("Branch_e29ef8cb-6b73-4418-83e1-6bb5cdbdba25_$LookupField");
var BranchValue =BranchDropDown.options[BranchDropDown.selectedIndex].text
if(BranchValue == "000"){
alert("Branch cannot be 000. Please select the appropriate branch number from the list");
return false ;
}else {
return true;
}
}
Please note, this should be the text, not value for the drop down list.
Otros consejos
Try using below code:
function PreSaveAction() {
var BranchValue = document.querySelector("select[title^='Branch'][id$='$LookupField']").value;
//Check what is the default value of lookup drop-down by inspecting element and use that value in below condition (In my case it is "0")
if(BranchValue == "000") {
alert("Branch cannot be 000. Please select the appropriate branch number from the list");
return false ;
} else {
return true;
}
}
Update from comments:
If .value
does not work for you then you can use one of the other options available to get the selected option from dropdown. Check below references for the same.