Exit a javascript loop and return false when validating a form variable against a list (array) [duplicate]

I am attempting to validate a form for inventorying boxes.

The form provides a suggested box number to the user (the next unused number in a list that is not necessarily sequential) but also allows the user to choose an arbitrary box number.

For example, the list of currently in-use box numbers might be 1,2,3,5,10. The form provides the suggested first available box number (4 in this case) to the user, but also provides a text field for the user to select a different number.

If the user enters their own number, I need to verify that it isn’t already in use. In the example above, if the user enters 3 I need to inform them that the number they have chosen is already assigned to a box.

The in_use boxes are passed in to the form from a MySQL query as the Jinja variable {{in_use_box_nums}}

My script correctly prevents users from selecting the “choose my own #” option but leaving the field blank, but it currently seems like the second check succeeds (an alert is displayed when you enter a duplicate box number) but the form submits anyway.

Code:

function validateForm() {
    // The box number they entered 
    let box_num = document.forms["add_box_form"]["box_num"].value;
    // The box_num_type value from radio buttons ("arbitrary" or "next_available")
    let box_num_type = document.forms["add_box_form"]["box_num_type"].value;
    // If the box number is empty, but the type is not "next_available" prevent form submission
    if (box_num == "" && box_num_type != "next_available"){ 
        alert ("A box number is required");
        return false;
    }
    // Construct the boxes_in_use array from the Jinja-supplied SQL results
    const boxes_in_use = {{in_use_box_nums|safe}}
    //loop through the box numbers to check if the entered number (box_num) is in the array
    boxes_in_use.forEach( element => {
         if (element == box_num){
             alert ("The box number you have selected is already in use.");
             return false;

         }
    });
}

I don’t think you need the form HTML, but I can include it if someone disagrees.

I’ve tried inserting a break into the script in the loop after the value test, but then it fails to prevent form submission.

What I was expecting was that when the user-selected box_num matched one of the numbers in the array, an alert would be shown, form submission would be prevented, and the user would be able to correct the error. Currently I’m getting an alert but the form submits.