Create a Masked Input Control (cont.)

Polishing the Mask
You have now successfully created a masked textbox by taking into account all the textbox's behaviors and creating a control that's safe to use in Web applications—and that works with all major browsers. You can extend this technique to restrict textbox entry for e-mail addresses, zip codes, phone numbers…almost anything you could possibly need while developing Web applications.

The code in the final example is by no means meant to be the final code that you use to mask textboxes. For example, here are some suggestions for polishing the code:

  • Create a single JavaScript function that will mask a given textbox in one step, instead of having to independently assign the onkeypress, onchange, and onpaste event handlers.
  • Make the mask into an HTML behavior (for use in IE only).
  • Create a generic way to apply masking based on the naming convention for textboxes. This avoids the need to assign all functions on every textbox.
  • Modify the functions in this article to be able to handle masking of multiple textboxes on the same HTML page.

Nicholas C. Zakas is a User Interface Designer for Web applications at MatrixOne, Inc. Nicholas works primarily as a client-side developer using JavaScript, DHTML, XML and XSLT. He can be reached via e-mail at nicholas@nczonline.net.


Don't Forget Paste Validation

Introduction

Introduction Exploring Textbox Behavior Blocking Keystrokes
Dealing with Special Keys Dealing with Cut, Copy, Paste, and Undo Don't Forget Paste Validation
Polishing the Mask    


 






 TALK BACK
Creating a masked edit text box in DTHML is not as straightforward as it seems at first. Do you know of other masked edit examples? Join the discussions at web.dhtml.scripting to get answers, make comments, or help others with their problems.
Click here to Join
Get the Code

MSDN DHTML Events Reference

MSDN DHTML Input Element Reference

Mozilla DOM Event Interface

Netscape DevEdge, "Form Validation with JavaScript 1.2 Regular Expressions"

Netscape DevEdge, "Form Validation with JavaScript 1.0"

MSDN Behaviors Reference





 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement