如何出演文本字段的一部分?像1234 **** **** 1234

发布于 2025-01-19 07:47:27 字数 212 浏览 4 评论 0原文

当用户输入信用卡号时,我想在上面的文本字段中饰演某个部分以进行安全。我该怎么做?

enter image description here

While the user is entering the credit card number, I want to star a certain part of the text field as above for security. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

檐上三寸雪 2025-01-26 07:47:27

我找到了解决方案,首先,你们中必须添加该textfield的委托
添加Textfield后,您必须添加以下代码,

func textFieldDidChangeSelection(_ textField: UITextField) {
    if (textField.text?.count ?? 0) > 4 && (textField.text?.count ?? 0) < 15{
        let firstFourElement = String(self.textFieldEmail.text?.prefix(4) ?? "")
        switch textField.text?.count ?? 0 {
        case 5:
            self.textFieldEmail.text = firstFourElement + " *"
        case 7:
            self.textFieldEmail.text = firstFourElement + " **"
        case 8:
            self.textFieldEmail.text = firstFourElement + " ***"
        case 9:
            self.textFieldEmail.text = firstFourElement + " ****"
        case 10:
            self.textFieldEmail.text = firstFourElement + " **** *"
        case 12:
            self.textFieldEmail.text = firstFourElement + " **** **"
        case 13:
            self.textFieldEmail.text = firstFourElement + " **** ***"
        case 14:
            self.textFieldEmail.text = firstFourElement + " **** **** "
        default:
            print("TEST")
        }
    }
}

I found the Solution, first of you have to add Delegate for that TextField,
After Adding TextField you have to add below code,

func textFieldDidChangeSelection(_ textField: UITextField) {
    if (textField.text?.count ?? 0) > 4 && (textField.text?.count ?? 0) < 15{
        let firstFourElement = String(self.textFieldEmail.text?.prefix(4) ?? "")
        switch textField.text?.count ?? 0 {
        case 5:
            self.textFieldEmail.text = firstFourElement + " *"
        case 7:
            self.textFieldEmail.text = firstFourElement + " **"
        case 8:
            self.textFieldEmail.text = firstFourElement + " ***"
        case 9:
            self.textFieldEmail.text = firstFourElement + " ****"
        case 10:
            self.textFieldEmail.text = firstFourElement + " **** *"
        case 12:
            self.textFieldEmail.text = firstFourElement + " **** **"
        case 13:
            self.textFieldEmail.text = firstFourElement + " **** ***"
        case 14:
            self.textFieldEmail.text = firstFourElement + " **** **** "
        default:
            print("TEST")
        }
    }
}
意中人 2025-01-26 07:47:27

我认为没有默认的 HTML 关键字,这里我为您做了一个简单的 JS 覆盖输入,看看它是否适合您的需求:

HTML:

<input type="text" id="text1" />

JS:

var ccnum = "";
var displaytext = "";

$(document).ready(function() {
    $("#text1").keydown(function(e) {
        if (e.which == 8)
        {
            event.preventDefault();
            ccnum = ccnum.substring(0, ccnum.length - 1);
            refineText();
        }
    });
    
    $("#text1").keypress(function(e) {
        event.preventDefault();
    
        var ch = String.fromCharCode(e.which);
        ccnum += ch;
        refineText();
        
        $("#text1").val(displaytext);
    });
    
    $("#text1").keyup(function(e) {
        if (e.which == 8)
        {
            $("#text1").val(displaytext);
        }
    });

});

function refineText()
{
    displaytext = ccnum;
    if (ccnum.length > 4)
    {
        displaytext = displaytext.substring(0, 4);
        var maxlength = ccnum.length - 4;
        if (maxlength > 8) maxlength = 8;
        
        for (i = 0; i < maxlength; i++)
            displaytext += "*";
        
        if (ccnum.length > 12)
            displaytext += ccnum.slice(12);
    }
}

I don't think there's a default HTML keyword for this, here I made a simple JS override input for you, see if it suits your needs:

HTML:

<input type="text" id="text1" />

JS:

var ccnum = "";
var displaytext = "";

$(document).ready(function() {
    $("#text1").keydown(function(e) {
        if (e.which == 8)
        {
            event.preventDefault();
            ccnum = ccnum.substring(0, ccnum.length - 1);
            refineText();
        }
    });
    
    $("#text1").keypress(function(e) {
        event.preventDefault();
    
        var ch = String.fromCharCode(e.which);
        ccnum += ch;
        refineText();
        
        $("#text1").val(displaytext);
    });
    
    $("#text1").keyup(function(e) {
        if (e.which == 8)
        {
            $("#text1").val(displaytext);
        }
    });

});

function refineText()
{
    displaytext = ccnum;
    if (ccnum.length > 4)
    {
        displaytext = displaytext.substring(0, 4);
        var maxlength = ccnum.length - 4;
        if (maxlength > 8) maxlength = 8;
        
        for (i = 0; i < maxlength; i++)
            displaytext += "*";
        
        if (ccnum.length > 12)
            displaytext += ccnum.slice(12);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文