How to Validate Whitespaces in Input form Using JQuery

The below code is using the jQuery validation plugin to validate an email input field. The jQuery validation plugin is a widely used library that makes it easy to add validation to forms using JavaScript.

Email: {
    required: {
        depends:function(){
            $(this).val($.trim($(this).val()));
            return true;
        }
    },
    email: true
}

The first rule being applied is the “required” rule. This rule ensures that the field is not empty before the form is submitted. However, in this case, the rule is being customized with an additional “depends” function.

The “depends” function is used to specify additional logic that should be executed before the validation rule is applied. In this case, the function is being used to remove any spaces from the input field before the validation is done.

The function is using the jQuery .val() method to get the current value of the input field. The $.trim() method is then used to remove leading and trailing spaces from the value. The .val() method is called again to set the input field value without spaces.

The “email” rule is being applied to the field. This rule is used to ensure that the field contains a valid email address.

Overall, this code is ensuring that the email field is not empty and contains a valid email address without any leading or trailing spaces.