Click here to Skip to main content
15,881,882 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.1K   645   46  
To provide a better user experience using MaskEdit, a textbox with restrictions, auot-trim box, and tip box, supports IE/Firefox.
/************************************************************
 * Javascript Lib V1.2
 * CONTACT: jerry.wang@clochase.com
 *
 * This is the Auto-Trim box
 *
 * InitializeTrimBox( "TextboxID" );
 * InitializeTrimBox( null );
 ************************************************************/

$import( "String.js" );
$import( "Firefox.attachEvent.js" );

//-------------------------------------------------------------------------
// Function Name    :InitializeTrimBox
// Parameter(s)     :ctrl           The ID of the textbox [can be null], or instance
// Memo             :If the ctrl is null, this function will Initialize
//                  :all the textboxes on the page
//-------------------------------------------------------------------------
function InitializeTrimBox(ctrl)
{
    if( ctrl != null )
    {
        var obj = typeof(ctrl) == "string" ? document.getElementById(ctrl) : ctrl;
        if( obj == null )
        {
            alert( "Error:ctrl {" + ctrl + "} does not exist!" );
            return false;    
        }
        
        __TrimBoxAddEventHandler(obj);
        return true;
    }
    
    var aryObject = document.getElementsByTagName("input");
    for( var i = 0; i < aryObject.length; i++)
    {
        var obj = aryObject[i];
        __TrimBoxAddEventHandler(obj);
    }
    
    return true;
}

//-------------------------------------------------------------------------
// Function Name    :__TrimBoxAddEventHandler
// Parameter(s)     :obj           The object of the textbox
// Memo             :Add event handle for the textbox
//-------------------------------------------------------------------------
function __TrimBoxAddEventHandler(obj)
{
     if( obj.type != "text" )
        return;
        
    obj.attachEvent( 'onchange', (function(p) 
        { 
	        return function()
	        {
	            p.value = p.value.trim();            
	        };    			
        }) (obj)
        );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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