Jquery inputmask casing uppercase/lowercase

2019-05-31 14:56发布

Im using jquery inputmask

How can I use below code to any input text with out a mask? any letter inputted will force to uppercase.

Inputmask.extendAliases({
  uppercase: {
            mask: '' // any letters
            definitions: {
                '*': {
                    casing: "upper" //
                }
        }
});

js

$('.uppercase').inputmask({casing:'upper'}); 
$('.lowercase').inputmask({casing:'lower'}); 

html

<input type="text" class="uppercase" />
<input type="text" class="lowercase" />

3条回答
对你真心纯属浪费
2楼-- · 2019-05-31 15:12

You can bind directly on input component using JavaScript:

<input type="text" class="uppercase" onkeyup="this.value = this.value.toUpperCase();" />
<input type="text" class="lowercase" onkeyup="this.value = this.value.toLowerCase();" />

If you're working with HTML5 you can use oninput parameter:

<input type="text" class="uppercase" oninput="this.value = this.value.toUpperCase();" />
<input type="text" class="lowercase" oninput="this.value = this.value.toLowerCase();" />

Or... as you're using jQuery, this works perfectly using css class:

<script>
    $(document).on('input', '.uppercase', function(){
       this.value = this.value.toUpperCase();
    });

    $(document).on('input', '.lowercase', function(){
       this.value = this.value.toLowerCase();
    });
</script>

Same thing as answer above, however changing values using pure jQuery:

<script>
    $(document).on('input', '.uppercase', function(){
       $(this).val($(this).val().toUpperCase());
    });

    $(document).on('input', '.lowercase', function(){
       $(this).val($(this).val().toLowerCase());
    });
</script>

Live example:

$(document).on('input', '.uppercase', function() {
  $(this).val($(this).val().toUpperCase());
});

$(document).on('input', '.lowercase', function() {
  $(this).val($(this).val().toLowerCase());
});
body {
  display: grid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>

<label>UpperCase JS:</label>
<input type="text" onkeyup="this.value = this.value.toUpperCase();" />
<hr>
<label>LowerCase JS:</label>
<input type="text" onkeyup="this.value = this.value.toLowerCase();" />
<hr>
<label>UpperCase jQuery:</label>
<input type="text" class="uppercase" />
<hr>
<label>LowerCase jQuery:</label>
<input type="text" class="lowercase" />


Reference:

查看更多
何必那么认真
3楼-- · 2019-05-31 15:14

.uppercase {
  text-transform: uppercase;
}

.lowercase {
  text-transform: lowercase;
}

.capitalize {
  text-transform: capitalize;
}
<input class="uppercase" />
<input class="lowercase" />
<input class="capitalize" />

Use css.

查看更多
贪生不怕死
4楼-- · 2019-05-31 15:19

You can use @guradio css and just uppercase results on form submit on serverside or with JavaScript before submit.

Or just read https://github.com/RobinHerbots/Inputmask there examples, you can use regexp to limit only to uppercase letters.

查看更多
登录 后发表回答