简体   繁体   中英

Jquery InputMask dynamic validation

Possible to make the input mask to enter both digit and character 'x' , but the input only accept a single 'x' regardless of the position.

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/bindings/inputmask.binding.min.js"></script>

<input type="text" data-inputmask-mask="9999">

This is what I currently working on , just wonder if input mask can do what I need , and else I have to use other way round.

Below is the scenario I wanted to achieve , the format must be in 4 character.

11x1
1x11
232x
x982

jsfiddle

<input type="text" id='word' maxlength="4">

$('#word').keyup(function(e){
    if($(this).val().length==3 && $(this).val().includes('x'))
    $(this).val($(this).val()+'x')  
})

Test cases

  • At least one letter should be 'x'

  • First 3 string may be letters, so last letter should be 'x'

  • If first 3 string contains x, no issue

Update it if you want to allow only 'numbers' and 'x'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM