Skip to content Skip to sidebar Skip to footer

Make Form Fields Optional With Javascript Validation

I have JavaScript form validation functions like so: function validate_name(field) { if (field == '') return 'Please enter the name.\n'; return ''; } function validate_sp

Solution 1:

You can do that by capturing the results separately, and testing with an OR grouping.

function validate_fields(form)
{
    fail1 = validate_name(form.name.value);
    fail2 = validate_specialty(form.specialty.value);
    fail3 = validate_location(form.location.value);

    if (fail1 == "" || fail2 == "" || fail3 == "")     
        return true;
    else 
    { 
        alert(fail1 + fail2 + fail3); 
        return false;
    }
}

Post a Comment for "Make Form Fields Optional With Javascript Validation"