Javascript alert on submit if text field is empty

html, javascript, jquery

Solution

Just make sure you return false in there somewhere-

$('#orderform').submit(function() {
    if($('#initials').val() == ''){
        alert('Please fill out your initials.');
        return false;
    }
});

Problem

So i want to alert the user if they submit the form with an empty text field HTML: ``` <form id="orderform"> <input type="text" name="initials" id="initials" maxlength="3"> <p class="center"> <input type="image" src="#" id="submitbutton" name="submit" value="Place Order"> </p> </form> ``` Javascript: ``` $('#orderform').submit(function() { if($('#initials').length == 0){ alert('Please fill out your initials.'); } }); ```

Original source