gpt4 book ai didi

jquery - 如何在使用 jquery 掩码时用 '*' 替换 ssn 的初始数字

转载 作者:搜寻专家 更新时间:2023-10-31 08:35:53 25 4
gpt4 key购买 nike

如何在使用 jquery 掩码时用 '*' 替换 ssn 的初始数字

例如:123-45-6789将作为 ***-**-6789 插入,但是当我们获取值时,我们应该得到 123456789

最佳答案

给定 HTML 如下:

<form id="ssn_form">
<input name="ssn" id="ssn" type="text">
<button id="submit">Submit</button>
</form>​

您可以将 SSN 存储在模糊的数据属性中并屏蔽该值。然后在表单提交上,您可以在提交表单之前将值重新填充到输入中:

var retrieveValue = function(ev){
var $this = $(this),
val = $this.data('value');

if (val) {
$this.val(val);
}
},
hideValue = function(ev){
var $this = $(this);

$this.data('value', $this.val());
$this.val($this.val().replace(/^\d{5}/, '*****'));
};

$('#ssn').focus(retrieveValue);

$('#ssn').blur(hideValue);

$('#ssn_form').submit(function(ev){
ev.preventDefault();
retrieveValue.call($('#ssn')[0], ev);
$(this).submit();
});

See demo

关于jquery - 如何在使用 jquery 掩码时用 '*' 替换 ssn 的初始数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com