Form Validation plugin with jquery

Form validation plugin with Jquery

// Form Validation plugin v1.0
(function ( $ ) {

      /* Options need to pass this plugin 
         required:'required'
         maxlength:anynumber
         onlyalphabet:true|false
         specialcharacternotallowed:true|false
         email:true|false
         phonenumber:true|false   
      */
    $.fn.formValidation = function( options ) {
      
      this.result = {required:true,alphanumeric:true,maxlength:true,onlyalphabet:true,specialcharacternotallowed:true,email:true,phonenumber:true};
      this.fieldValue = $(this).val();

      this.message = '';
      // Validation object
      var settings = $.extend({}, options );
      
      
      // Required field
      if(settings.required == 'required'){
            if(!this.fieldValue){
                  this.result.required =  false;
                  this.message = 'This field is required';
            }
            else{
                  this.result.required =  true;
            }
            
      }
      // Maxlength test
      if(settings.maxlength){
            if((this.fieldValue.length <= settings.maxlength)){
                  this.result.maxlength = true;
            }
            else{
                  this.result.maxlength = false;
                  this.message = 'Max length is ' + settings.maxlength;
            }
      }
      // Only alphabets are allowed
      if(settings.onlyalphabet){
            var regEx_onlyalphabet = /^[a-zA-Z ]*$/;
            if(regEx_onlyalphabet.test(this.fieldValue)){
                  this.result.onlyalphabet = true;
            }
            else{
                  this.result.onlyalphabet = false;
                  this.message = 'Plese enter only alphabets.'
            }

      }

      // Special character are not allowed
      if(settings.specialcharacternotallowed){
            var regEx_specialcharacternotallowed = /[$-/:-?{-~!"^_`\[\]]/;
            if(regEx_specialcharacternotallowed.test(this.fieldValue)){
                  this.result.specialcharacternotallowed =false;
                  this.message = 'Special character are not allowed.'
            }
            else{
                  this.result.specialcharacternotallowed = true;
            }

      }

      // Email validation
      if(settings.email && this.fieldValue){
            var regEx_email = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
            if(regEx_email.test(this.fieldValue)){
                  this.result.email = true;
            }
            else{
                  this.result.email = false;
                  this.message = 'Please enter a valid email id.'
            }
      }

      // Phone number validation
      if(settings.phonenumber && this.fieldValue){
            var regEx_phonenumber = /^([0|\+[0-9]{1,5})?([0-9]{9})$/;
            if(regEx_phonenumber.test(this.fieldValue)){
                  this.result.phonenumber = true;
            }
            else{
                  this.result.phonenumber = false;
                  this.message = 'Please enter valid phone number.'
            }

      }
      
      for(var x in settings){
            if(this.result.hasOwnProperty(x)){
                  if(!this.result[x]){
                        $(this).closest('.form-group').addClass('error');
                        $(this).closest('.form-group').find('.error-message').html(this.message);
                        return false;
                  }    
            }
      }
      $(this).closest('.form-group').removeClass('error');
      $(this).closest('.form-group').find('.error-message').html('&nbsp;');
      return true;


    };

}( jQuery ));

How to use the plugin?

$("#firstname-a").on('blur',function(){
      $('#firstname-a').formValidation({required:'required',maxlength:24,onlyalphabet:true});
});
$("#username-a").on('blur',function(){
      $('#username-a').formValidation({required:'required',maxlength:24,specialcharacternotallowed:true});
});
$("#role-a").on('blur',function(){
      $('#role-a').formValidation({required:'required'});
});
$('#user-management-add').on('click',function(){
      var firstname_validation    = $('#firstname-a').formValidation({required:'required',maxlength:24,onlyalphabet:true});
      var lastname_validation     = $('#lastname-a').formValidation({maxlength:24,onlyalphabet:true});
      var username_validation     = $('#username-a').formValidation({required:'required',maxlength:24,specialcharacternotallowed:true});
      var role_validation         = $('#role-a').formValidation({required:'required'});
      var contact_validation      = $('#contact-a').formValidation({maxlength:10,phonenumber:true});
      var email_validation        = $('#email-a').formValidation({maxlength:50,email:true});            
      var description_validation  = $('#stationdescription-a').formValidation({maxlength:50});

      if(firstname_validation && lastname_validation && username_validation && description_validation && email_validation && role_validation && contact_validation ){
            return true;
      }
      else{
            return false;
      }
});

Html

<div class="form-group">
      <label>First name</label>
      <input type="text" class="form-control" id="firstname-e">
      <p class="error-message">Error message here.</p>
</div>