2 回答
TA贡献1851条经验 获得超4个赞
我不建议你使用这种技术。而不是这些使用一些外部库
$("#masked").on("change paste keyup", function() {
let inputValue = $(this).val();
if (inputValue.split("/")[0] > 12) {
$(this).val(12);
}
});
$("#masked").mask("00/00");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<div id="app">
<h3>Masked Input Plugin as MM/YY.</h3>
<table border="0">
<tbody>
<tr>
<td>
<input id="masked" type="text" value="" placeholder="MM/YY" data-mask="00/00"
data-mask-clearifnotmatch="true"/>
</td>
<td>typing 0221 but getting 02/21</td>
</tr>
</tbody>
</table>
</div>
TA贡献1804条经验 获得超7个赞
<!DOCTYPE HTML>
<html>
<form action="">
<div>
<label for="cc">Expiration Date</label>
<!-- Set via HTML -->
<input id="cc" type="text" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])\/(1[5-9]|2\d)" data-valid-example="05/18"/>
</div>
</form>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/masking-input.js" data-autoinit="true"></script>
</html>
添加回答
举报