1

I am (very) new to Sharepoint and am a little stuck.

I have created a list containing a number of dropdown columns which are all required fields.

Sharepoint defaults the list to the first item on each dropdown which is not great.

So I have added an entry of to each dropdown list to appear first.

The problem is I need to find a way to prevent people from selecting that value.

I looked around a lot online but couldn't get anything to work.

The code below looks like it should work just fine, but for some reason is not doing anything!

Would anybody be able to offer some advice?

<script src="../../SiteAssets/jquery.min.js"></script>
<script>
function PreSaveAction()
{
    return formVal(); //calls the validation on save
}
function formVal() {
    if ($("[title='Name']").val() == '<Select From List>') {
alert('Error: Please select a name before submitting');
        return true;
    }
    else {


    return false;
}
</script>
2
  • 1
    Make sure that value of first element of dropdown is "<Select From List>" .. Commented Sep 25, 2017 at 12:41
  • Hi. Yes that is the case. The first element on the dropdown is <Select From List> Commented Sep 25, 2017 at 12:57

1 Answer 1

1

As you mentioned that you have added an entry as <Select From List> to the parent list so that each lookup drop down will have first value as <Select From List>.

If your lookup field name is Name then you can validate it by following jQuery selector expression.

function formVal() {
    if($("select[title='Name'] option:selected").text() == "<Select From List>"){
        alert('Error: Please select a name before submitting');
        return false;
    }
    else {
        return true;
    }
}
6
  • Hi. Appreciate the response. I tried this and it made the screen go blank! I saved the code and added a link to a CEWP. Is this the wrong way to add this to the page? Commented Sep 25, 2017 at 13:38
  • Updated my code. You can either directly add the code in CEWP inside <script type="text/javascript"></script> tag OR add the code to a text file and then refer it in CEWP e.g. /SiteAssets/NewItemForm.txt
    – Yayati
    Commented Sep 25, 2017 at 14:14
  • Thanks for trying. Not sure what I am doing wrong - I just get a blank screen once i add the file to the CEWP. My code now looks like this which looks correct. <script src="../../SiteAssets/jquery-1.11.2.min.js"></script> <script type="text/javascript"> function PreSaveAction() { return formVal(); //calls the validation on save } function formVal() { if($("select[title='Name'] option:selected").text() == "<Select From List>"){ alert('Error: Please select a name before submitting'); return false; } else { return true; } } <script> Commented Sep 25, 2017 at 14:23
  • Another way is to add the code in Script Editor web part. You can remove CEWP and add Script Editor web part. Click on Edit Snippet and paste your entire code in that. Then click on Insert and Save the Page. Note: All the javascript code should be written inside <Script> element.
    – Yayati
    Commented Sep 25, 2017 at 14:41
  • Hmm.. I have tried this and still I result in a white screen. Could it be something to do with the site settings I wonder? It is strange that my other little code files all work fine! Commented Sep 25, 2017 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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