Click here to Skip to main content
6,822,613 members and growing! (17,820 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Input mask - a script that automatically enters a specified character at a certain point in a text box.

By webProgrammer

A script that will mask an input field - can be used on multiple fields on the same form with diferent characters, at different points in the text box, all using the same 10 line function.
JavascriptWin2K, WinXP, Win2003, Dev
Posted:11 Aug 2003
Updated:19 Aug 2003
Views:134,280
Bookmarked:39 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 5.47 Rating: 4.14 out of 5

1
3 votes, 14.3%
2
3 votes, 14.3%
3
4 votes, 19.0%
4
11 votes, 52.4%
5

Introduction

Some times when creating forms, we like to use something that's called an "input mask". An input mask is something that, (most times) as you're typing in (let's say) a text box, certain characters will automatically be entered into the textbox.

For example: if I had a text box that is supposed to hold a phone number and I want the phone number to be formatted like so "111-111-1111", in such a case the input mask will automatically enter the "-" character after the third and sixth characters, in order that the user does not have to do it.

In my script, those characters MUST be included.

Script breakdown and explanation

<form name="form" action="" method="post">

<input name="Field1" value="" type="text" 

onKeyUp="javascript:return mask(this.value,this,'3,6','-');"

onBlur="javascript:return mask(this.value,this,'3,6','-');"

style="font-family:verdana;font-size:10pt;width:110px;"

maxlength="11">

</form>

The first 3 lines are (hopefully) self understood, however I will explain them: the <form> tag can be edited, although all that needs editing is the action="".

The <INPUT> tag is set to call the JavaScript function (below) named mask, it passes 4 parameters to the function,

  1. str - the value of the current textbox control,
  2. textbox - the actual textbox object, (so that the value can be set)
  3. loc - a string of multiple locations to place the specified character,
  4. delim - the character (delimiter) that you want to use a separator.

The maxlength value is set to a length of 11 since I would like 9 characters and 2 "-" characters.

The mask function is called in 2 places (1: onKeyUp, so that as the user types, the field will be edited, 2: onBlur, this is not needed, but it's just in case)

Below is the function line by line explained:

function mask(str,textbox,loc,delim){

Begins the function with a name mask, and sets 4 parameters, as mentioned above.

var locs = loc.split(','); 

Creates an array out of multiple numbers (to add each separator on its own).

for (var i = 0; i <= locs.length; i++){

Begins a loop through the array of locations.

for (var k = 0; k <= str.length; k++){

Begins a nested loop through each character to check if the delimiter is already there.

if (k == locs[i]){

Begins a conditional statement that checks to see what character number we are holding.

if (str.substring(k, k+1) != delim){

Begins a nested conditional statement if the character we're holding is the correct character location, and checks to see if it is the same character as the delimiter.

str = str.substring(0,k) + delim + str.substring(k,str.length)

if the character is not the same as the delimiter, it will cut the value in half and insert the delimiter

}

}

}

}
textbox.value = str

This sets the value of the textbox control being edited

}

There you have it, the complete script. If you would like to test the script, click here.

Conclusion

Nothing in the function needs editing, and only the last 2 parameters of the textbox that calls the function should be edited, although a user can do as he wills.

Notes:

This script works well in Netscape 6+ and IE 4+. In Netscape 4, after the JavaScript edits the field, the cursor will go to the beginning of the field (this can be fixed).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

webProgrammer


Member

Occupation: Web Developer
Location: United States United States

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
GeneralHelp with Jquery Pinmembersmarika17:53 11 Jan '10  
GeneralA small problem. Pinmemberwer@_op0323:23 24 Sep '08  
GeneralRe: A small problem. Pinmemberwer@_op0323:39 24 Sep '08  
Generalrequired field validator Pinmemberrsgrady21:54 12 Oct '07  
GeneralRe: required field validator PinmemberwebProgrammer18:26 16 Oct '07  
GeneralRe: required field validator Pinmemberrsgrady22:22 19 Oct '07  
GeneralRe: required field validator PinmemberwebProgrammer12:28 21 Oct '07  
QuestionEditing? PinmemberUmer Khan1:32 19 Jul '07  
AnswerRe: Editing? PinmemberChris_CP14:34 7 Aug '07  
QuestionMore Than one Delimare Pinmemberyaseencarter3:32 18 Jul '07  
Question(232) 232-2323 how to do thta PinmemberUmer Khan0:54 18 Jul '07  
GeneralThanks! Pinmembersweettomandjerry16:45 4 Apr '07  
GeneralGreat tool! Pinmembergogetsome5:49 28 Jul '06  
GeneralJumps to beginning of field Pinmemberdaddion7:46 19 Sep '05  
GeneralRe: Jumps to beginning of field Pinmemberdaddion7:54 19 Sep '05  
GeneralRe: Jumps to beginning of field Pinmemberdaddion8:37 19 Sep '05  
Generalfirefox PinsussAnonymous18:52 12 Nov '04  
GeneralNetscape PinsussAnonymous11:49 20 Jan '04  
GeneralRe: Netscape PinmemberwebProgrammer12:29 20 Jan '04  
GeneralRe: Netscape PinsussAnonymous4:52 21 Jan '04  
GeneralRe: Netscape PinmemberwebProgrammer8:43 21 Jan '04  
GeneralAnother bug Pinmemberbm_ross18:30 15 Sep '03  
QuestionRe: Another bug PinmemberUmer Khan2:44 28 Sep '07  
GeneralDeleting Field information PinmemberEric Bennion4:52 19 Aug '03  
GeneralRe: Deleting Field information PinmemberEric Bennion5:04 19 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 19 Aug 2003
Editor: Smitha Vijayan
Copyright 2003 by webProgrammer
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project