0

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;

    }

}

}

0

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.

| improve this answer | |
  • Thanks Jerry this worked like a charm. Would you be able to work some magic and provide me one code for both checks? If the Suspicious Activity Amount field >0, then attachment is required and the branch cannot be 000? Appreciate your help here. – 3LV1S May 28 at 16:23
  • I tried using your code with mine, but nothing happens when the form is saved. Added an edit with the code. – 3LV1S May 28 at 16:47
  • @Elvis, Did you have any error message ? Text value should be working in this case. – Jerry_MSFT May 29 at 0:50
  • You code works perfectly, thank you. What i was hoping for is clubbing my first code to check the attachment, and the one you gave me for the lookup column together, and that's where I'm facing a challenge. i added the edits i made in combining them both, to my main question. Issue is the attachment one works but your code doesn't, when i put them together in one script. I think it may have to do with how its placed or maybe the parenthesis. Hoping for your inputs. – 3LV1S May 29 at 1:49
  • Hi Jerry, will you be able to help me in combining the code or tell me what I'm doing wrong please? – 3LV1S May 29 at 16:47
0

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.

  1. Get selected value in dropdown list using JavaScript

  2. jQuery Get Selected Option From Dropdown.

| improve this answer | |
  • Hi Ganesh, Jerry is right. Value does not work for drop down list. Thanks for your help though. – 3LV1S May 28 at 13:42
  • Hi Elvis, .value does work for me. However there are multiple other ways to get the selected option from dropdown. Check my updated answer, you can use any of the method from it. – Ganesh Sanap May 28 at 16:00
  • Thank you sir! This taught some new ways to do this. Appreciate it. – 3LV1S May 28 at 17:33
  • Great, I am glad it helped you. Please Upvote and accept it as an answer as it helped you and it will help other to find the best solution in future. – Ganesh Sanap May 28 at 17:49
  • Also, to combine both conditions, you can check any first condition using if and check other condition in both if and else block of first condition check. – Ganesh Sanap May 28 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.