0

I am not being able to validate a simple HTML form using jquery. Please help. The following is my code.

Please note i am very new to jquery,so this might be a silly question to ask. Any help would be greatly appreciated

<script type="text/javascript">
  function validate(){

    alert("1");

     $("#form1").validate({

     rules: { 
         name: "required", 
         age: "required", 
         id: "required", 
    },
    messages: {
        name: " Enter username", 
        age: " Enter age",
        id: " Enter id",

      },
      submitHandler: function(form) {
          alert('3');
            form.submit();

      }

   });     
  }; 
  </script>
  <title>Spring MVC Form Handling</title>
  </head>
  <body>

  <h2></h2>
  <form method="POST" action="addStudent.do" id=form1>
  <div id="form-content">
    <fieldset>
  <div class="fieldgroup">

      <label for="name">First Name</label> 
      <input type="text"  id="name">
  </div>

  <div class="fieldgroup">   
    <label for="age">Age</label>
    <input type="text"  id="age">
  </div>   

 <div class="fieldgroup"> 
   <label for="id">Id</label>
   <input type="text" id="id">
 </div> 

    <div class="fieldgroup">

             <input type="submit" value="Register" id="submit" class="submit"    onclick="validate()">  
    </div>

   </fieldset>
    </div>
  </form>


  </body>
Sparky
  • 98,165
  • 25
  • 199
  • 285
Shivayan Mukherjee
  • 766
  • 3
  • 10
  • 33

2 Answers2

0

try this

<script>
       $.validator.setDefaults({
            submitHandler: function() { alert("submitted!"); }
        });

        $(document).ready(function() {

           $("#form1").validate({

            rules: { 
             name: "required", 
             age: "required", 
             id: "required", 
           },
            messages: {
            name: " Enter username", 
            age: " Enter age",
            id: " Enter id",

           }        
       });    

      }
</script>



<input type="text" name="name" id="name">
 <input type="text" name="age" id="age">
 <input type="text" name="id" id="id">
<input type="submit" value="Register" id="submit" class="submit"> 
Voonic
  • 4,667
  • 3
  • 27
  • 58
  • 1
    Thanks for the help guys .. Finally i discovered what was wrong in it,all the fields had the "id" attribute which was not being recognized in Jquery. changing the same to "name" solved it for me. – Shivayan Mukherjee Sep 13 '13 at 12:55
0

try to change the form to

<form id="Form1" method="POST" action="addStudent.do">
.
.
.

copy and paste it and you will see it is working !

xDeveloper
  • 23
  • 4