0

I have multiple Sharepoint dropdowns validation but I will show only two on my code. I can't seem to find where the problem is. The logic is if the user choose from any dropdown values <> "yes" then user must fill-out the respective exception multiline textbox.

The first If Else statement is working but the second is not. I appreciate any help.

<script>
function PreSaveAction()
{
    return formVal(); //calls the validation on save
}
function formVal() {
    if ($("[title='Acknowledgement']").val() == 'Yes') {
        return true;
    }
    else {
        if ($("[title='Acknowledgement Exception']").val().length) //checks if not empty use length > X to stipulate min char length allowed
        {
            return true;
        }
        else { //textbox is empty
            alert('Error: Please add the required exception into the box before submitting');
        }
    }
return false;

if ($("[title='Preparation Review']").val() == 'Yes') {
        return true;
    }
    else {
        if ($("[title='Preparation Exception']").val().length) //checks if not empty use length > X to stipulate min char length allowed
        {
            return true;
        }
        else { //textbox is empty
            alert('Error: Please add the required preparation exception into the box before submitting');
        }
    }

    return false;
}
</script>
2

There are two issues,

  1. You are returning true immediately after a condition is passed. That prevents any further validations from being run.

  2. There is a return false statement after first if-else block that prevents execution of any code below it.

Try this:

<script>
function PreSaveAction()
{
    return formVal(); //calls the validation on save
}
function formVal() {
    bool validationPassed = true;

    if ($("[title='Acknowledgement']").val() == 'Yes') {
        // return true; // If you return true here, none of the below conditions get validated.
    }
    else {
        if ($("[title='Acknowledgement Exception']").val().length) //checks if not empty use length > X to stipulate min char length allowed
        {
            // return true;
        }
        else { //textbox is empty
            alert('Error: Please add the required exception into the box before submitting');
            return false;
        }
    }

    // return false; // This condition here was preventing the script below from executing.

    if ($("[title='Preparation Review']").val() == 'Yes') {
            // return true;
        }
    else {
        if ($("[title='Preparation Exception']").val().length) //checks if not empty use length > X to stipulate min char length allowed
        {
            // return true;
        }
        else { //textbox is empty
            alert('Error: Please add the required preparation exception into the box before submitting');
            return false;
        }
    }

    // Moved *return false* from here to else block.

    return validationPassed;
}
</script>
  • Thank you so much, I tried your code but it is nothing validating either dropdown boxes. – normsp Apr 11 '16 at 14:25
  • Did you try to debug the script? You might be able to find something there. – Nisarg Apr 11 '16 at 14:34
  • Thank you all for your effort and time. I found a simple solution using the built in validation and using formulas. summit7systems.com/… – normsp Apr 11 '16 at 15:28
  • Yeah that was always there! My bad I didn't ask if you had checked that! You need to post that as an answer and mark it accepted. – Nisarg Apr 11 '16 at 15:33
  • I need to revisit your code since there is a limit of validation using the summit link. I can't debug the script since I am doing this on Sharepoint 365 hosted in the Cloud by Microsoft. Any idea why nothing is validating with your code? – normsp Apr 13 '16 at 21:06
2

Please mark Nisargs answer as the correct one, as he pointed out your errors.

It is always worthwhile to spend time on simplifying your code.

I just spent a week bringing the 3000 line iCSR JavaScript library down to 400 lines, code is now easier to read, easier to maintain, and I solved 10 bugs along the way.

I did not test this code below, beware of typos

<script>
function PreSaveAction() {
    function fieldValue(fieldTitle){
        return document.querySelector("input[title='" + fieldTitle + "']").value;
        //yes! we just killed the need for 90Kb in an extra library here
        //no more code needed to load jQuery on this Form
        //return $("[title='" + fieldTitle + "']").val(); 
    }
    function validate( fieldName, fieldException ) {
        var validated = fieldValue(fieldName) === 'Yes' 
                        && 
                        fieldValue(fieldException).length > 0;
        if (!validated) 
             SP.UI.Status.addStatus('Error',String.format('Please add the required {0} exception into the box before submitting', fieldException));
        return validated;
    }

    return validate('Acknowledgement', 'Acknowledgement Exception') 
            && 
            validate('Preparation Review', 'Preparation Exception');
}
</script>

You can add color to the Status message as well: https://msdn.microsoft.com/en-us/library/office/ff408240(v=office.14).aspx

  • Thank you so much. I tried your code but it did not validate either dropdown when pressing submit. – normsp Apr 11 '16 at 14:23
  • Thank you so much. I tried your code but it did not validate either dropdown when pressing submit. – normsp Apr 18 '16 at 21:14
  • Then there is a bug/typo in the code and you have to debug it – Danny '365CSI' Engelman Apr 19 '16 at 7:56

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.