You might take a look at the mask plugin by Andrew Powell in the jQuery UI
Labs
http://wiki.jqueryui.com/mask

Demo:
<http://wiki.jqueryui.com/mask>
http://jquery-ui.googlecode.com/svn/branches/labs/mask/demos/mask/default.html

Source:
http://jquery-ui.googlecode.com/svn/branches/labs/mask/ui/ui.mask.js

It's based on the jquery.maskedinput.js plugin by Josh Bush (
http://digitalbush.com/projects/masked-input-plugin/).

- Richard

On Wed, Aug 12, 2009 at 4:07 PM, Brett Ritter <swift...@swiftone.org> wrote:

>
> My workplace has a lot of people doing manual entry.  I'm looking to
> provide some friendly automatic formatting of data as they type it.
>
> I have working code (thus far) but I want to ensure I'm doing things
> in a sane way (there's a surprising amount you can do insanely that
> still works :) ).
>
> Here's a function below that transforms a phone number - the worker
> enters the digits(only), the field reflects (and could also accept)
> the fully formatted version (e.g. (123) 456-7890 ).
> I'm building this towards a plugin model (ala masked input with happy
> coexistence with validate)
>
>        $("#example").keyup(function(){
>                var val = $(this).val();
>                if(/^[-\d\(\) ]*$/.test(val)){
>                        var base = val.replace(/[-\(\) ]/g, '');
>                        var size = base.length;
>                        if(size < 3){  // Area code
>                                // Leave unchanged
>                        }
>                        else if (size < 6){
>                                val = "(" + base.slice(0,3) + ") " +
> base.slice(3);
>                        }
>                        else if(size < 11){
>                                val = "(" + base.slice(0,3) + ") " +
> base.slice(3,6) + "-" + base.slice(6);
>                        }
>                        $(this).val(val);
>                }
>        });
>
> Comments appreciated.
> --
> Brett Ritter / SwiftOne
> swift...@swiftone.org
>

Reply via email to