0

I've set all rules correctly, yet they seem to be ignored completely when I submit the Form.

Here is my code:

$(document).ready(function() {

  $('#formValidation').validate({
    rules: {
      name: {
        required: true,
        minlength: 2,
        lettersonly: true,
      },
      email: {
        required: true,
        minlength: 5,
        email: true,
      },
      feedback: {
        required: true,
        minWords: 5,
      },
    }
    messages: {
      name: {
        required: "Please enter your name",
      },
    },
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>
<form id="formValidation" onload="validate()">
  <p>Name: <br/><input type="text" name="name" /> </p>
  <p>Email:<br/><input type="email" name="email" /></p>
  <p>Feedback:<br/><input type="text" name="feedback" id="textfield" />
    <input type="submit" id="submit" onsubmit="return formValidation()" /> </p>
</form>

3
  • There is no need for onsubmit event in the submit input element and for onload in the form element. Commented Apr 26, 2015 at 15:13
  • Also, might sound strange, but are you loading the jquery validate plugin? Commented Apr 26, 2015 at 15:14
  • I am loading the plugin. I followed LShetty's advice and got the same result. It seems to ignore everything and submit the form regardless Commented Apr 26, 2015 at 17:48

1 Answer 1

1

So, all you need is the following.

$(document).ready(function () {

    $('#formValidation').validate({ 
        rules: {
            name: {
                required: true,
                minlength: 2,
                lettersonly:true,
            },
            email: {
                required: true,
                minlength: 5,
                email:true,
            },
            feedback: {
                required: true,
                minWords: 5,
            }
        },
        messages: {
            name:{
                required:"Please enter your name",
            }
        }
    });  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.js"></script>
<form id="formValidation" method="post">
    <p>Name: <br/><input type="text" name="name" /> </p>
    <p>Email:<br/><input type="email" name="email" /></p>
    <p>Feedback:<br/><input type="text" name="feedback" id="textfield"/>
    <input type="submit" id="submit"/> </p>
</form>

  • Removed unnecessary/extra ,s from your script.
  • Remove inline event handlers
Sign up to request clarification or add additional context in comments.

2 Comments

Hey LShetty, thanks for replying, I followed your feedback accordingly and the result is the same when I run it . However it works when I run the code snippet on Stackoverflow
I would guess that either your links to the validate script are not working, or you've attached something else to the submit event of the form that is intercepting those events.

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.