How to validate form in jQuery using jQuery Form Validator
There are multiple jquery plugins to validate forms but i am going to tell you how to validate a simple form in jquery, even though this plugins have so many attribute option rules to validate form such as :
- maxWords
- minWords
- rangeWords
- letterswithbasicpunc
- alphanumeric
- lettersonly
- nowhitespace
- ziprange
- zipcodeUS
- integer
- vinUS
- dateITA
- dateNL
- time
- time12h
- phoneUS
- phoneUK
- mobileUK
- phonesUK
- postcodeUK
- strippedminlength
- email2 (optional TLD)
- url2 (optional TLD)
- creditcardtypes
- ipv4
- ipv6
- pattern
- require_from_group
- skip_or_fill_minimum
- accept
- extension
Here is a simple form which i will validate using jQuery.
- {!! Form::open(array('route' => 'your_route','method'=>'POST','files'=>true,'id'=>'myform')) !!}
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Name:</strong>
- {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Email:</strong>
- {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Phone:</strong>
- {!! Form::text('phone', null, array('placeholder' => 'Mobile No','class' => 'form-control')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12">
- <div class="form-group">
- <strong>Details:</strong>
- {!! Form::textarea('details', null, array('placeholder' => 'Details','class' => 'form-control','style'=>'height:100px')) !!}
- </div>
- </div>
- <div class="col-xs-12 col-sm-12 col-md-12 text-center">
- <button type="submit" class="btn btn-primary">Submit</button>
- </div>
- {!! Form::close() !!}
Now include this script before closing body tag.
- <script src="http://demo.expertphp.in/js/jquery.validate.min.js"></script>
- <script>
- $(document).ready(function () {
- $('#myform').validate({ // initialize the plugin
- rules: {
- name: {
- required: true
- },
- email: {
- required: true,
- email: true
- },
- phone: {
- required: true
- },
- details: {
- required: true
- }
- }
- });
- });
- </script>
Now you can validate your form using jQuery. Click here to see the example to validate form using jQuery.