We have pin numbers in the following format:

45 674 25 910

Our original requirement was to give the users the ability to enter their pin with or without spaces,

In other words, if the pin is 10 digits without spaces, we would like to accept it as valid input.

Similarly, if the pin is 13 digits (with the three spaces) we would also like to accept the input as valid.

If the digits are less than 10 with or without spaces, or more than 13 with spaces, we would like to throw an error that input is invalid.

The script below satisfied the above requirements:

```
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#btnSearch").click(function () {
            var result = true;
            if ($('#pin').val().replace(/ /g, '').length == 10) {
                result = true;
            }
            else {
                result = false;
                alert("Invalid");
                return false;
            }
            return result;
        });
    });
</script>

```

However, management has decided to change the requirement to ask that spaces be automatically added while the user is entering the pin numbers.

In other words, users can enter the pin numbers with spaces or they can enter the pin numbers without spaces but that spaces be automatically added while they are typing the pin numbers.

Any ideas how to modify the script above?

Better yet, is there an example that I can modify to meet our requirements?