Click here to Skip to main content
15,892,927 members
Articles / Web Development / ASP.NET

Restricting input by MaskEdit in IE / Firefox

Rate me:
Please Sign up or sign in to vote.
4.85/5 (13 votes)
5 Nov 2006CPOL2 min read 118.4K   645   46   22
To provide a better user experience using MaskEdit, a textbox with restrictions, auot-trim box, and tip box, supports IE/Firefox.

Introduction

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

Demo

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.

HTML
<head>
    // ......
    <script language="javascript" src="JSLib/Global.js" 
      type="text/javascript"></script>
    <script language="javascript" src="JSLib/Textbox.Restriction.js" 
      type="text/javascript"></script>
    <script language="javascript" src="JSLib/Textbox.MaskEdit.js" 
      type="text/javascript"></script>
    <script language="javascript" src="JSLib/Textbox.Trim.js" 
      type="text/javascript"></script>
    <script language="javascript" src="JSLib/Textbox.Tip.js" 
      type="text/javascript"></script>
    // ......
</head>

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

JavaScript
function OnPageLoad()
{
    InitializeTrimBox();    

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

    // 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.");
}

Deficiencies

  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.

History

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

License

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


Written By
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)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions

 
GeneralIt cannot work on Safari Pin
jatu_p6-Jan-10 0:32
jatu_p6-Jan-10 0:32 
Generalalphanumeric Pin
Filipe Freitas4-Dec-08 7:17
Filipe Freitas4-Dec-08 7:17 
General5 from me Pin
shokisingh3-Oct-07 2:24
shokisingh3-Oct-07 2:24 
GeneralAdd new escape characters Pin
placido19-Jul-07 5:19
placido19-Jul-07 5:19 
GeneralRe: Add new escape characters Pin
Jerry.Wang19-Jul-07 14:58
Jerry.Wang19-Jul-07 14:58 
GeneralLicense Pin
rossie17-Jul-07 19:10
rossie17-Jul-07 19:10 
GeneralRe: License Pin
Jerry.Wang17-Jul-07 19:53
Jerry.Wang17-Jul-07 19:53 
GeneralRe: License Pin
rossie17-Jul-07 22:05
rossie17-Jul-07 22:05 
incidentally since my post, i've been playing about with a similar sort of thing as what you have done here. But i've managed to minimize the code down to one method.

It works by only allowing text through that fit a given regular expression. After each keypress, the regular expression is evaluated. If there is a match then the key press is allowed and the text is updated, if there is no match then the keypress is disallowed and the text is left as the original.

This is handy because you could make an input textbox only accept numeric keypresses for instance. The regular expression match pattern in this case would be "^\d+$"

just rig it up to your onkeypress handler for your textbox. please note javascript's regular expressions are a bit weird, you have to do a funny \\ thing to represent a \ or a /\ combination to represent a \

onkeypress="RegExTextBoxKeyPressHandler(this, '^\\s*[-+]?[0-9]*\\.?[0-9]?[0-9]?\\s*$')"

function RegExTextBoxKeyPressHandler(element, pattern)
{
    if (!event) var event = window.event;
    var keyCode = (event.which) ? event.which : event.keyCode;

    if (keyCode && element && pattern) {
        
        var range = document.selection.createRange();
        var bookmark = range.getBookmark();
        var caret_pos = bookmark.charCodeAt(2) - 2;        
         
        var original = element.value;
        var current = original.substring(0, caret_pos) + 
            String.fromCharCode(keyCode) + 
            original.substring(caret_pos + range.text.length, original.length);

        // floating point with 2 decimal places.
        //var re = new RegExp("^\\s*[-+]?[0-9]*\\.?[0-9]?[0-9]?\\s*$");
        var re = new RegExp(pattern);
        var matches = re.exec(current);
        if(!matches) 
            event.returnValue = false; 
            
         
    }
}


This prolly looks a bit hacky - but thats javascript through and through isn't it? Big Grin | :-D

Looks like a real bodge to get the current cursor pos. But the code on a whole is okay for a first iteration, feel free to improve it code monkeys out there! Did you use that returnValue = false; in your code to cancel the keypress?
Generalimplementation Pin
tanachai8-Jun-07 4:26
tanachai8-Jun-07 4:26 
GeneralOpera Pin
Leandro Souza25-Nov-06 1:59
Leandro Souza25-Nov-06 1:59 
GeneralRe: Opera Pin
Jerry.Wang26-Nov-06 17:47
Jerry.Wang26-Nov-06 17:47 
GeneralRe: Opera Pin
Jerry.Wang27-Nov-06 18:10
Jerry.Wang27-Nov-06 18:10 
GeneralInput direction Pin
OxYgEn_Pt23-Nov-06 1:55
OxYgEn_Pt23-Nov-06 1:55 
GeneralRe: Input direction Pin
Jerry.Wang23-Nov-06 14:27
Jerry.Wang23-Nov-06 14:27 
GeneralRe: Input direction Pin
OxYgEn_Pt27-Nov-06 23:31
OxYgEn_Pt27-Nov-06 23:31 
Generalgood job! Pin
mybios17-Nov-06 17:06
mybios17-Nov-06 17:06 
GeneralExcellent !! 5 Globe Pin
Tittle Joseph6-Nov-06 1:13
Tittle Joseph6-Nov-06 1:13 
Generalvery goood Pin
winart19-Oct-06 22:53
winart19-Oct-06 22:53 
GeneralGood work jery Pin
ravihd18-Oct-06 17:44
ravihd18-Oct-06 17:44 
GeneralOne suggestion Pin
Hardy Wang18-Oct-06 7:25
Hardy Wang18-Oct-06 7:25 
GeneralRe: One suggestion Pin
Jerry.Wang18-Oct-06 15:22
Jerry.Wang18-Oct-06 15:22 
Generalgood work! Pin
phic15-Oct-06 16:46
phic15-Oct-06 16:46 

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

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