0

On clicking the Go button it says all good message even if invalid values are filled in the boxes.

<!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <meta name="robots" content="noindex, nofollow">
      <meta name="googlebot" content="noindex, nofollow">
      <meta name="viewport" content="width=device-width, initial-scale=1">



        <script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.js"></script>  


        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>


        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js"></script>



      <title></title>




    <script type="text/javascript">


    $(document).ready(function () {

        jQuery.validator.addMethod('integer', function (value, element, param) {
            return (value != 0) && (value == parseInt(value, 10));
        }, 'Please enter a non zero integer value!');

        $('#myform').validate({
            rules: {
                field1: {
                    required: true,
                    integer: true
                },
                field2: {
                    required: true,
                    integer: true
                }
            },
            submitHandler: function (form) { // for demo
                alert('valid form');
                return false;
            }
        });

                $('#go').click(function(){ 

                        if (!$("#myform").validate()) 
                        { 

                            // Not Valid
                            alert('missing some data');
                            return false;

                        } 
                        else 
                        {    

                            <!-- $("#myform").submit() -->
                            alert('all good');
                            return false;

                        }
                });

    });



    </script>


    </head>

    <body>
        <form id="myform">
            <input type="text" name="field1" />
            <br/>
            <input type="text" name="field2" />
            <br/>
            <input id="go" value="GO" type="button" />
        </form> 

    </body>

    </html>
1
  • jqueryvalidation.org/validate says the return from validate() is the validator, and objects in javascript are always truthy Commented May 8, 2018 at 17:08

1 Answer 1

1

Not "if" .validate() - this is the validator object and will always be true since it exists.

Use .valid() for any boolean testing:

$('#go').click(function() { 
    if (! $("#myform").valid()) { ....
Sign up to request clarification or add additional context in comments.

1 Comment

you are awesome :) thanks saved me , oh Gold for this plugin ...definitely no doubts about it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.