Question

I want to know how I can separate numbers or make sure there are spaces between numbers the user enters, e.g. input 0797238189 is converted to 079 723 8189.

Was it helpful?

Solution

You can use a javascript library like Mask.

For a phone number you can try a mask like this new Mask("### ### ####");
So for a complete example :

<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>

OTHER TIPS

If you want it on the client side, then you can use jquery like so which will add a space after the user enters a certain number of characters into your text field:

$('#myTextFieldId').change(function(){
    if ($(this).length == 3) {
        $(this).val($(this).val() + ' ');
    } else if ($(this).length == 7) {
        $(this).val($(this).val() + ' ');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top