jQuery validate - how do I ignore letter case?

后端 未结 5 2094
无人共我
无人共我 2021-02-20 16:08

Probably a goofy question, but I can\'t seem to find anything in google land. I simply need one of my methods to ignore the case of the entered field. I am doing a city name mat

5条回答
  •  余生分开走
    2021-02-20 16:34

    You could even create a more generic jQuery validation rule which can be reused to perform a case insensitive comparison like

    $.validator.addMethod("equalToIgnoreCase", function (value, element, param) {
            return this.optional(element) || 
                 (value.toLowerCase() == $(param).val().toLowerCase());
    });
    

    which can then be used for the following two sample inputs

    
    
    

    like

    $("form").validate({
        rules: {
            firstname: {
               equalToIgnoreCase: "input[name=username]"
            }
        },
        messages: {
            firstname: {
               equalToIgnoreCase: "The 'firstname' must match the 'username'"
            }
        });
    

提交回复
热议问题