Verify form validation

Hello, I have a form that I want to run through form validation, and then submit. How can I check if the following function returns true in order to know that everything has been checked and then sent?

I created a script for testing

http://jsfiddle.net/WHGq2/

MODIFIED CODE

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
     <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

    <script type="text/javascript">
            $(function(){
        $("#form").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                        },
                phone: {
                    equired: true,
                    phone: true
                        }
                    },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
                phone: "Please provide a valid phone number.",
            }
        })  


    $('#form').submit(function(e){
        // Stop the form actually posting
        e.preventDefault();




        // I want to be able to do the check here if the form has passed validation
        if( $(this).valid() ) {
        // Stop the form actually posting

        // Send the request
        $.post('/submit.php', {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            message: $('#message').val()
        }, function(d){
            alert("Thank you for submitting your request someone will contact your shortly.");
        });
        }
    });
});
    </script>
+4
source share
4 answers

You can call the valid () method to validate jquery. how

if($('#form').valid())

In fact, you can declare a validate function when loading an html page and just check if it is valid or not during submission

$(document).ready(function(){

$('#form').validate(rules...messages...);

In case of using rules and messages: -

  rules: {
            name: {
               required: true  
            },
            email: {
                required: true,
                email: true
                    },
            phone: {
                required: true,
                phone: true                       }
                },
        messages: {
            name: { required : "Please let us know who you are."},
           email: {required : "A valid email will help us get in touch with you.",email : "A valid email will help us get in touch with you."},
            phone: {required:"Please provide a valid phone number.",phone:"Please provide a valid phone number."}
        }

, .

$('#form').submit(function(evt) {
    evt.preventDefault();

    .....

    if( $('#form').valid() ) {
       //submit the form via ajax
    } else {
       //show errors
    }
 });

});

, , : -

 <input type="text" name="email" />
+5

, required phone equired . . .

$(function() {

    $('#form').validate(.......);

    $('#form').submit(function(e) {
        e.preventDefault();

        .....

        if( $(this).valid() ) {
           //submit the form via ajax or otherwise
        } else {
           //report errors
        }
    });

});

JSFIDDLE DEMO

+2

valid , - .

$('#form').validate({...});

if ($('#form').valid()) {
    // do your thing
}

http://jqueryvalidation.org/valid

0

, submitHandler, , .

    <script type="text/javascript">
            $(function(){
    $('#form').submit(function(e){

        $("#form").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                        },
                phone: {
                    equired: true,
                    phone: true
                        }
                    },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
                phone: "Please provide a valid phone number.",
            },
            submitHandler: function() {  
                      // Stop the form actually posting
                       e.preventDefault();

                     // Send the request
                   $.post('/submit.php', {
                   name: $('#name').val(),
                   email: $('#email').val(),
                   phone: $('#phone').val(),
                   message: $('#message').val()
                   }, function(d){
                        alert("Thank you for submitting your request someone will contact your shortly.");
                   }); 
            }
    });
});
});
    </script>
0

Source: https://habr.com/ru/post/1548399/


All Articles