输入数字时使用空格


Zend spaces when inputting digits

我想知道如何区分数字或确保用户输入的数字之间有空格,例如输入0797238189转换为079 723 8189

你可以使用像Mask这样的javascript库。

对于电话号码,您可以尝试像这样的遮罩new Mask("### ### ####");
对于一个完整的例子:

<html>
    <head>
        <script src="./masks.js"></script>
        <script>
        function init(){
            document.frmExample.reset();
            oPhoneMask = new Mask("### ### ####");
            oPhoneMask.attach(document.frmExample.phone_number);
        }
        </script>
    </head>
    <body onload="init();">
        <form name="frmExample" method="get" onsubmit="return false;">
            <b> Phone Number:</b><br />
            <input type="text" name="phone_number" value="" /><br />
            Generic: ### ### ####
        </form>
    </body>
</html>

如果你想在客户端使用它,那么你可以像这样使用jquery,它会在用户输入一定数量的字符后添加一个空格到你的文本字段:

$('#myTextFieldId').change(function(){
    if ($(this).length == 3) {
        $(this).val($(this).val() + ' ');
    } else if ($(this).length == 7) {
        $(this).val($(this).val() + ' ');
    }
});