2

I have 13 forms and every form with 5 to six inputs.
Below code is working perfectly but have to repeat for all inputs.

$('#formID').validate({
    rules:{
        inputName:{
            required:true,
            normalizer: function (value) {
                //Trim the value of element for whitespaces
                return $.trim(value);
             }
        }
    },
    messages:{
        inputName: {
            required: "Please fill some description"
        }
    }
});

To trim and validate for each input i tried this so far

$('form input, form textarea').each(function (index, element) {
        var testInput =$(element).attr("name");
        // console.log(testInput+'ehllo');
        $('form').validate({
            rules:{
                testInput:{
                    normalizer: function (value) {
                        //Trim the value of element for whitespaces
                        return $.trim(value);
                    }
                }
            }
        });

    });

But this isn't working

Sparky
  • 98,165
  • 25
  • 199
  • 285
Ashutosh Sharma
  • 434
  • 1
  • 9
  • 20
  • Follow the same idea for the `input` elements using a generic selector and enclosing the `.rules()` method within an `.each()`. See option **2b** here: https://stackoverflow.com/a/9056425/594235 – Sparky Aug 08 '18 at 21:37
  • jQuery validation support the use of a global normalizer in its latest release 1.17.0. See https://github.com/jquery-validation/jquery-validation/pull/1905 for usage example. – Arkni Aug 09 '18 at 12:14

1 Answers1

2

Please check this format.

Validating multiple forms on the same page

example http://jsfiddle.net/K6Tkn/

$('form').each(function() {
    $(this).validate({
        rules:{
            inputName:{
                required:true,
                normalizer: function (value) {
                    //Trim the value of element for whitespaces
                    return $.trim(value);
                 }
            }
        },
        messages:{
            inputName: {
                required: "Please fill some description"
            }
        }
    });
    });
jvk
  • 2,133
  • 3
  • 19
  • 28