【问题标题】:Mask First 5 Characters of SSN when using angular-ui-mask使用 angular-ui-mask 时屏蔽 SSN 的前 5 个字符
【发布时间】:2018-11-06 23:00:09
【问题描述】:

我使用 angular-ui-mask 以 123-45-6789 格式显示 ssn,但我也想在输入框失去焦点时隐藏前 5 个字符,并在输入框获得焦点时显示所有数字。

我的 HTML

<input class="btnDropdown" 
       id="phoneTextInput" 
       ui-mask="{{uimaskpattern}}" 
       ui-mask-placeholder-char=""
       ng-focus="applymask()"
       ng-blur="phoneForm['Phone_'+$index].$viewValue = removemask(contact.contactDetails.attributeDetails.contact)"
       md-minlength="10" 
       md-maxlength="10" 
       name="Phone_{{$index}}" 
       ng-model="contact.contactDetails.attributeDetails.contact">

角度代码:

uimaskpattern 的初始值 = "999-99-9999"

//When input gets focus
public applymask() {
    this.uimaskpattern = "999-999-9999";
}

//When input loses focus
public removemask(ssn) {
    if (ssn) {
        console.log(ssn);
        this.uimaskpattern = "XXX-XXX-9999";
        let formstring = 'XXX-XXX-'+ssn.slice(6, ssn.length);
        console.log(formstring);
        return formstring;
    }
}

我面临的问题是,在模糊时,如果用户输入 123456789,那么在模糊时,文本变为 XXX-XX-1234,而不是 XXX-XX-6789,即,而不是 6789,附加的数字是第一个4,如果我只是用 {{}} 打印返回的字符串,它会正确显示

【问题讨论】:

    标签: javascript jquery angularjs angularjs-directive


    【解决方案1】:

    如下改变ng-blur

    ng-blur="removemask(contact.contactDetails.attributeDetails.contact)"
    

    并删除掩码功能如下并立即检查。它可能工作

    public removemask(ssn) {
        if (ssn) {
            console.log(ssn);
            this.uimaskpattern = "XXX-XXX-9999";
            let formstring = 'XXX-XXX-'+ssn.slice(6, ssn.length);
            console.log(formstring);
            ssn=formstring;
        }
    }
    

    【讨论】:

    • 我得到了字符串,但是在 ui 上,而不是前四个字符被附加到 XXX-XX- 而不是最后四个,我也尝试了你的代码。
    • 你能提供任何演示链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多