JQuery Form validation example 1

From CoderLearner
Jump to: navigation, search

What is form validation in jQUery

There are many ways you can validate form input from end-users. Using jQuery form validation is completely fast and easy, you don't need to write a lengthy code as you do using javascript. This example shows you how to use jQuery to validate form elements.

Example 1

This is a completed code for this tutorial.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Validate</title>
      <script 
         type="text/javascript" 
         src="/lib/jquery-1.7.2.min.js">				 
      </script>

      <script 
         type="text/javascript" 
         src="/lib/jquery.validate.js">
      </script>

      <style>
         .error {
            border: 1px solid red;
            color:red;
         }
      </style>
      <script type="text/javascript">
         $(document).ready(function(){
            $('form[name="frm_user_infor"]').validate({
               rules: {
                  street: {required:true, minlength:5, maxlength:10},
                  city: {required:true,minlength:2, maxlength:10},
                  zip: {required:true,minlength:5, maxlength:12},
                  state: {required:true,minlength:2, maxlength:10},
                  c_user:{required:true,minlength:2, maxlength:15},
                  c_number: {required:true,minlength:14, maxlength:16},
                  c_secret: {required:true,minlength:3},
                  c_month: {required:true,minlength:2},
                  c_year: {required:true,minlength:2}
               },
               message: {
                  street: {
                     required: 'Street can not be empty.',
                     minlength: 'Street is too short',
                     maxlength: 'Street is too long'
                  },
                  city: {
                     required: 'City can not be empty.',
                     minlength: 'City is too short',
                     maxlength: 'City is too long'
                  },
                  zip: {
                     required: 'Zip code can not be empty.',
                     minlength: 'zip is too short',
                     maxlength: 'zip is too long'
                  },
                  state: {
                     required: 'State can not be empty.',
                     minlength: 'State is too short',
                     maxlength: 'State is too long'
                  },
                  c_user: {
                     required: 'User name can not be empty.',
                     minlength: 'User name is too short',
                     maxlength: 'User name is too long'
                  },
                  c_number: {
                     required: 'Card number can not be empty.',
                     minlength: 'Card number is too short',
                     maxlength: 'Card number is too long'
                  },
                  c_secret: {
                     required: 'Card secret code can not be empty.',
                     minlength: 'Card secret code number is too short'
                           
                  },
                  c_month: {
                     required: 'Card expiration month can not be empty.',
                     minlength: 'Card expiration month is too short'
                  },
                  c_year: {
                     required: 'Card expiration year can not be empty.',
                     minlength: 'Card expiration year is too short'                   
                  }
               },
               submitHandler:function(){
                  if(confirm("Are you sure you want to submit this course?"))
                  {
                     var URL='/eshop/lib/transactioncentral.php';
                        
                     var street = $('input[name="street"]').val();
                     var city = $('input[name="city"]').val();
                     var zip  = $('input[name="zip"]').val();
                     var state = $('input[name="state"]').val();
                     var c_name = $('input[name="c_user"]').val();
                     var c_number = $('input[name="c_number"]').val();
                     var c_secret = $('input[name="c_secret"]').val();
                     var c_month = $('input[name="c_month"]').val();
                     var c_year = $('input[name="c_year"]').val();
                     var amount = $('input[name="Amount"]').val();
                     var dataf = 'action=send&street='
                        +street
                        +'&city='
                        +city
                        +'&zip='
                        +zip
                        + '&state='
                        +state 
                        +'&c_name='
                        +c_name 
                        + '&c_number=' + c_number 
                        + '&c_secret='+ c_secret
                        + '&c_month=' + c_month 
                        + '&c_year='+ c_year 
                        +'&amount='+amount;
                        
                     $.ajax({
                        url: URL,
                        type: 'POST',
                        data:dataf,
                        success:function(data){
                           alert('data');
                        }
                     });
                  }
                  else {
                     alert('bad');
                     //form.submit();
                  }
                  return false;
               }
            });
         });
      </script>
   </head>
   <body>
      <form name="frm_user_infor" id="frm_user_infor" method="post">
         <span class="title">Process Check Out</span>
         <table cellspacing="8">
            <tr>
               <td></td>
               <td>Billing Address</td>
            </tr>
            <tr>
               <td>Street</td>
               <td><input type="text" name="street" size="10" maxlength="15"/>
               </td>
            </tr>
            <tr>
               <td>City</td>
               <td><input type="text" name="city" size="10" maxlength="10"/>
               </td>
            </tr>
            <tr>
               <td>Zip Code</td>
               <td><input type="text" name="zip" size="10" maxlength="10"/>
               </td>
            </tr>
            <tr>
               <td>State</td>
               <td><input type="text" name="state" size="10" maxlength="15"/>
               </td>
            </tr>

            <tr>
               <td></td>
               <td>Card Information</td>
            </tr>
            <tr>
               <td>Name on Card</td>
               <td>
                  <input type="text" name="c_user" size="30" maxlength="30"/>
               </td>
            </tr>
            <tr>
               <td>Card Number</td>
               <td>
                  <input type="text" name="c_number" size="14" maxlength="17"/>
               </td>
            </tr>
            <tr>
               <td>Security Code</td>
               <td>
                  <input type="text" name="c_secret" size="4" maxlength="4"/>
               </td>
            </tr>
            <tr>
               <td>Expiration</td>
               <td>
                  Month<input type="text" name="c_month" size="2" maxlength="2"/>
                  Year<input type="text" name="c_year" size="2" maxlength="2"/>     
               </td>
            </tr>
            <tr>
               <td></td>
               <td><input type="submit" name="process" value="Place Order"/></td>
            </tr>
         </table>
      </form>
   </body>
</html>

Try It