简体   繁体   English

格式化银行帐户输入仅用于显示

[英]Formating bank account input only for display

I need to format a text input for bank number so that the numbers would look in this way: 我需要设置银行编号的文本输入格式,以便数字看起来像这样:

11 2490 1111 0000 0000 2222 3333 11 2490 1111 0000 0000 2222 3333

The problem is I just want to show this formated (with spaces) for the user but to send it to the server without it (11249011110000000022223333). 问题是我只想向用户显示此格式(带空格),但是将其发送给没有它的服务器(11249011110000000022223333)。 How can I achieve this? 我该如何实现?

EDIT 编辑

This is what I've written using formatter.js library ( http://firstopinion.github.io/formatter.js/index.html ): 这是我使用formatter.js库( http://firstopinion.github.io/formatter.js/index.html )编写的:

<script>
$(document).ready(function () {
        var formatted = new Formatter(document.getElementById('bank_account-0'), {
        'pattern': '{{99}} {{9999}} {{9999}} {{9999}} {{9999}} {{9999}} {{9999}}',
        'persistent': false
    });
});
</script>

You can use bankAccount.value.replace(/ /g, '') . 您可以使用bankAccount.value.replace(/ /g, '')

Code example: 代码示例:

 var bankAccount = document.getElementById('bank_account-0'), getNumber = document.getElementById('get_number'); new Formatter(bankAccount, { 'pattern': '{{99}} {{9999}} {{9999}} {{9999}} {{9999}} {{9999}} {{9999}}', 'persistent': false }); getNumber.onclick = function() { var num = bankAccount.value.replace(/ /g, ''); console.log(num); } 
 .input { width: 240px} 
 <script src="//firstopinion.github.io/formatter.js/javascripts/formatter.js"></script> <input type="text" class="input" id="bank_account-0" maxlength="26" pattern="\\d*"> <button id="get_number">Get number</button> 

You might consider changing the value each time the input gets unfocused or pasted value into: 您可能会考虑在每次输入未聚焦或粘贴到以下值时将值更改为:

var a = document.getElementById('yourInputId');
a.onblur = function() { this.value = this.value.replace(/ /g, ''); };

Hint: You might consider cutting the white-spaces on server side. 提示:您可以考虑减少服务器端的空格。 This might be a part of server-side validation. 这可能是服务器端验证的一部分。

Finally I decided to use simple PHP filter instead: 最后,我决定改用简单的PHP过滤器:

$element->addFilter('PregReplace', array( 'match' => '/\s/', 'replace' => '' )); 

That works fine with cleave.js library on the frontend and meet my requirements. 前端上的cleave.js库可以很好地满足我的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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