Skip to content Skip to sidebar Skip to footer

Is There A Way To Prevent A Form From Being Submitted If It Is Not Auto Completed?

I'm learning jquery and its awesome! Its very easy to do complex stuff but I'm unsure how to prevent someone from submitting a form(or warning them) if a autocomplete fails. I h

Solution 1:

Have the form loaded with the button as disabled. As you are filling up the form, you have a running tally of flags (class=validation) to notify when the form is ready, then you can enable the button.

There is no sense in having a workable button that will attempt to call the server if you already know that it should fail if not all the information is ready.

The example in the bottom will work if you fill up all three boxes with text. You can signify controls by having each textbox with a class append text "required" in red font using the appendTo() in jquery.

As long as there is a class validation on the html element, it will be required, so as we are entering data, it will remove those validation classes:

<html><head><scriptsrc="jquery.js"></script><scripttype="text/javascript">

    $(document).ready(function(){

        $("input[name^='test']").blur(function(){
             if ($(this).val() != "" && $(this).hasClass("validation")){
                $(this).removeClass("validation");
            }

            var check = $(".validation"); //checks to see if there is any more validationif (check.length == 0){
                $("#nowSubmit").prop("disabled", false);
            } else {
                $("#nowSubmit").prop("disabled", true);
            }
        });
    });
  </script></head><body><form><inputid="control1"name="test"type="textbox"class="validation" /><inputid="control2"name="test"type="textbox"class="validation" /><inputid="control3"name="test"type="textbox"class="validation" /><inputid="nowSubmit"type="submit"value="Click Here"disabled="true"/></form></body></html>

working example here: http://jsfiddle.net/hSatZ/

Solution 2:

You can use ajax request put on "onchange" event on autocomplete fields which would check if specific value is in db and will notify user about error if needed without submitting the form

Post a Comment for "Is There A Way To Prevent A Form From Being Submitted If It Is Not Auto Completed?"