lundi 6 mai 2019

How can you use form validation to go through multiple options?

The rest of the form validation worked fine, until I got to this: Sex must be a single character equal to M, m, F, or f

<script>
    function verify() {
        if (document.forms[0].age.value < 18) {
            alert ("Age must be between 18 and 30");
            return false;
        } 
        if (document.forms[0].age.value > 30) {
            alert ("Age must be between 18 and 30");
            return false;
        } 
        if (document.forms[0].age.value == "") {
            alert ("Age must be between 18 and 30");
            return false;
        } 
        if (document.forms[0].last.value == "") {
            alert ("Last name canot be empty.");
            return false;
        } 
        if (document.forms[0].sex.value !== "M" || "m" ||"F" ||"f")
            alert ("Please indicate your sex.");
            return false;
        } 
        alert ("Thank you for your submission.")
            return true;
    }
</script>

Expected: Pop ups for each issue with the form submission. I got a few before adding the bit about the sex validation.

Actual: Now no pop ups are occurring.

Aucun commentaire:

Enregistrer un commentaire