Click here to Skip to main content
11,409,922 members (59,114 online)
Click here to Skip to main content

Restricting input by MaskEdit in IE / Firefox

, 5 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
To provide a better user experience using MaskEdit, a textbox with restrictions, auot-trim box, and tip box, supports IE/Firefox.


There is a very useful control called CXTMaskEdit in XtremeToolkit used in VC++. I managed to replant the MaskEdit to JavaScript, then I started to create the JavSscript lib myself. The only purpose is to better the user's experience.

There are several kinds of textboxes I am providing for IE / Firefox.

  1. Textbox only accepts non-negative integer
  2. Textbox only accepts whole number
  3. Textbox only accepts currency (e.g., 14.22 ) and two decimal points
  4. MaskEdit accepts special strings such as postal code, telephone number etc.
  5. Textbox will auto-trim when it loses focus
  6. Textbox will show tip if it is empty


Here comes the demo, please try it yourself: Click here for the demo.

Using the code

You should copy the *.js in the /JSLib/ to your project, and there is a file named "help.txt" that describes which files you need to include in your HTML pages. The Global.js is necessary.

    // ......
    <script language="javascript" src="JSLib/Global.js" 
    <script language="javascript" src="JSLib/Textbox.Restriction.js" 
    <script language="javascript" src="JSLib/Textbox.MaskEdit.js" 
    <script language="javascript" src="JSLib/Textbox.Trim.js" 
    <script language="javascript" src="JSLib/Textbox.Tip.js" 
    // ......

And then you should initialize the textboxes when the page loads, for example:

function OnPageLoad()

    // all the first parameters of the functions below are the textbox id   
    // initialize textbox only accepts nonnegative integerBox
    InitializeTextbox( "txtAcceptNonnegativeIntegerBox", 

    // initialize textbox only accepts whole number
    InitializeTextbox( "txtAcceptWholeNumberBox", InputType.WholeNumber);

    // initialize textbox only accepts currency
    InitializeTextbox( "txtAcceptCurrencyBox", InputType.Currency);

    // initialize textbox only accepts Postal Code, such as "M1M 1M1"
    InitializeMaskEdit( "txtPostalCode", "$A$d$A $d$A$d");

    // initialize textbox only accepts telphone number, such as "(111)111-1111"
    InitializeMaskEdit( "txtTelPhone", "($d$d$d)$d$d$d-$d$d$d$d");

    // initialize textbox only accepts car number, such as "A*11111"
    InitializeMaskEdit( "txtCarNumber", "$$A*$d$d$d$d$d");

    // initialize textbox only accepts datetime, such as "2006-12-30"
    InitializeMaskEdit( "txtDateTime1", "$d$d$d$d-$d$d-$d$d");

    // initialize textbox only accepts datetime, such as "12/30/2006"
    InitializeMaskEdit( "txtDateTime2", "$d$d/$d$d/$d$d$d$d");

    // initialize tip box
    InitializeTipBox( "txtTipBox", 
      "If you do not change the password, left blank.");


  1. It does not support Chinese characters at this time. I would like to implement it but it is very difficult, I need to consider more.
  2. Since Firefox does not support onpaste / ondrag / ondrop / oncut / oncontentmenu events, the user can still input an invalid string by pasting, dropping, so this script is just used to provide a better experience, and it is necessary to use ASP.NET validation controls.


  • V1.0 2006-10-10: Created.
  • V1.1 2006-10-15
    • Improvement: Renamed InputType.
    • Fixed bug: If there is already a value for mask edit when page loads, the value will be removed.
    • Improvement: Changed the Escape character to $A $a $d.
    • Fixed bug: Should prevent content menu and cutting in mask edit in IE.
    • Improvement: Added the auto-trim textbox.
    • Improvement: Add the tip textbox.
  • V1.2 2006-11-05
    • Improvement: Add the JavaScript object browser.
    • Improvement: For the InitializeXXX functions, can pass either control ID or instance.
    • Fixed bug: If the mask edit gets focus, the input cursor will move to the first unfilled position.
    • Improvement: Added the AJAX class.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.

  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics

Contact: vcer(at)
Chinese Blog:

Comments and Discussions

GeneralIt cannot work on Safari Pinmemberjatu_p6-Jan-10 1:32 
Generalalphanumeric PinmemberFilipe Freitas4-Dec-08 8:17 
General5 from me PinmemberShoki3-Oct-07 3:24 
GeneralAdd new escape characters Pinmemberplacido19-Jul-07 6:19 
GeneralRe: Add new escape characters PinmemberJerry.Wang19-Jul-07 15:58 
GeneralLicense Pinmemberrossie17-Jul-07 20:10 
GeneralRe: License PinmemberJerry.Wang17-Jul-07 20:53 
GeneralRe: License Pinmemberrossie17-Jul-07 23:05 
Generalimplementation Pinmembertanachai8-Jun-07 5:26 
GeneralOpera PinmemberLeandro Souza25-Nov-06 2:59 
GeneralRe: Opera PinmemberJerry.Wang26-Nov-06 18:47 
GeneralRe: Opera PinmemberJerry.Wang27-Nov-06 19:10 
GeneralInput direction PinmemberOxYgEn_Pt23-Nov-06 2:55 
GeneralRe: Input direction PinmemberJerry.Wang23-Nov-06 15:27 
GeneralRe: Input direction PinmemberOxYgEn_Pt28-Nov-06 0:31 
Generalgood job! Pinmembermybios17-Nov-06 18:06 
GeneralExcellent !! 5 Globe PinmemberTittle Joseph6-Nov-06 2:13 
Generalvery goood Pinmemberwinart19-Oct-06 23:53 
GeneralGood work jery Pinmemberravihd18-Oct-06 18:44 
GeneralOne suggestion PinmemberHardy18-Oct-06 8:25 
GeneralRe: One suggestion PinmemberJerry.Wang18-Oct-06 16:22 
Generalgood work! Pinmemberphic15-Oct-06 17:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 6 Nov 2006
Article Copyright 2006 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid