1

I'm using metronic framework for my application. This metronic form allows white-spaces as valid with (tick) mark even if don't enter any character.

Until user enter the value field should be invalid.

Here sample link how it works http://keenthemes.com/preview/metronic/theme/admin_4/form_validation.html

Please go to "VALIDATION USING ICONS" form and type 3 or more spaces in nae field and then you can see tick mark icon for valid which is not valid truly.

Can you suggest how to resolve this issue.

klmuralimohan
  • 861
  • 4
  • 23
  • 44

1 Answers1

0

You can find this validation in file \assets\pages\scripts\form-validation.js.

First, add an additional method noSpace to validate whitespaces. Then you can modify the method which handles validation for #form_sample_2 which is handleValidation2. You just need to enter noSpace: true in rules for name

The entire code is given below:

// validation using icons

// Additional method to validate whitespaces
jQuery.validator.addMethod("noSpace", function(value, element) { 
  return value == '' || value.trim().length != 0; 
}, "No space please and don't leave it empty");

var handleValidation2 = function() {
    // for more info visit the official plugin documentation: 
    // http://docs.jquery.com/Plugins/Validation

    var form2 = $('#form_sample_2');
    var error2 = $('.alert-danger', form2);
    var success2 = $('.alert-success', form2);

    form2.validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",  // validate all fields including form hidden input
        rules: {
            name: {
                minlength: 2,
                required: true,
                noSpace: true
            },
            email: {
                required: true,
                email: true
            },
            email: {
                required: true,
                email: true
            },
            url: {
                required: true,
                url: true
            },
            number: {
                required: true,
                number: true
            },
            digits: {
                required: true,
                digits: true
            },
            creditcard: {
                required: true,
                creditcard: true
            },
        },

        invalidHandler: function (event, validator) { //display error alert on form submit              
            success2.hide();
            error2.show();
            App.scrollTo(error2, -200);
        },

        errorPlacement: function (error, element) { // render error placement for each input type
            var icon = $(element).parent('.input-icon').children('i');
            icon.removeClass('fa-check').addClass("fa-warning");  
            icon.attr("data-original-title", error.text()).tooltip({'container': 'body'});
        },

        highlight: function (element) { // hightlight error inputs
            $(element)
                .closest('.form-group').removeClass("has-success").addClass('has-error'); // set error class to the control group   
        },

        unhighlight: function (element) { // revert the change done by hightlight

        },

        success: function (label, element) {
            var icon = $(element).parent('.input-icon').children('i');
            $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
            icon.removeClass("fa-warning").addClass("fa-check");
        },

        submitHandler: function (form) {
            success2.show();
            error2.hide();
            form[0].submit(); // submit the form
        }
    });
}

Once this is done, minify the form-validation.js file and replace the existing form-validation.min.js file.

prinkpan
  • 2,117
  • 1
  • 19
  • 32