// JavaScript Document // tbMask v1.0 // // Textbox 'masking' on client-side in Internet Explorer. // // Written By John McGlothlin - April 17th, 2004 // // Mask Characters // 9 = Numeric only // A = Alpha only // X = Alphanumeric // // All other chars treated as literals. // // var CTRL_PASTE = 22; var CTRL_COPY = 3; var CTRL_CUT = 24; var TAB_KEY = 9; var DELETE_KEY = 46; var BACKSPACE_KEY = 8; var ENTER_KEY = 13; var RIGHT_ARROW_KEY = 39; var DOWN_ARROW_KEY = 40; var UP_ARROW_KEY = 38; var LEFT_ARROW_KEY = 37; var HOME_KEY = 36; var END_KEY = 35; var PAGEUP_KEY = 33; var PAGEDOWN_KEY = 34; var CAPS_LOCK_KEY = 20; var ESCAPE_KEY = 27; // -------------------------------------------------------------------- // Main function // Gets the current keystroke and deals with it....lol // -------------------------------------------------------------------- function tbBlur(textBox) { var mask = textBox.mask; var c = 0; var v = ''; // while(c < textBox.value.length) { while(c < mask.length) { currentMaskChar = mask.charAt(c); if(currentMaskChar == '9' || currentMaskChar == 'X' || currentMaskChar == 'A') v = v + '_'; else v = v + currentMaskChar; c++; } // while if (textBox.value == v) textBox.value = ''; } function tbMaxChar(textBox, Max) { if (textBox.value.length > (Max-1)) { aux = textBox.value; textBox.value = aux.substring(0,Max); alert("O número máximo de caracteres para este campo é: "+Max); } } function tbMask(textBox) { var keyCode = event.keyCode; // get character from keyCode....dealing with the "Numeric KeyPad" // keyCodes so that it can be used var keyCharacter = cleanKeyCode(keyCode); var retVal = false; // grab the mask var mask = textBox.mask; switch(keyCode) { case BACKSPACE_KEY: var c = getCursorPos(textBox); if(c > 0) { var currentMaskChar; // get next available char to delete except mask chars while(c > 0) { c--; currentMaskChar = mask.charAt(c); if(currentMaskChar == '9' || currentMaskChar == 'X' || currentMaskChar == 'A') { // found a spot.....replace that char with '_' var x = textBox.value.substring(0,c); var y = textBox.value.substring(c+1,textBox.value.length); textBox.value = x + '_' + y; setCursorPos(textBox,c); textBox.curPos = c; break; } // if } // while } // if break; case TAB_KEY: // keep track of cursor b4 tabbing out of field var c = getCursorPos(textBox); textBox.curPos = c; retVal = true; break; case HOME_KEY: // just move/keep track of cursor setCursorPos(textBox,0); textBox.curPos = c; break; case END_KEY: // just move/keep track of cursor setCursorPos(textBox,textBox.value.length); textBox.curPos = textBox.value.length; break; case ENTER_KEY: retVal = true; break; case DELETE_KEY: var c = getCursorPos(textBox); if(c > -1) { var currentMaskChar = mask.charAt(c); // only allow delete if it's a valid char if(currentMaskChar == '9' || currentMaskChar == 'X' || currentMaskChar == 'A') { var x = textBox.value.substring(0,c); var y = textBox.value.substring(c+1,textBox.value.length); textBox.value = x + '_' + y; setCursorPos(textBox,c); textBox.curPos = c; } // if } // if break; case LEFT_ARROW_KEY: // just move/keep track of cursor var c = getCursorPos(textBox); if(c > 0) { setCursorPos(textBox,c-1); textBox.curPos = c-1; } // if break; case RIGHT_ARROW_KEY: // just move/keep track of cursor var c = getCursorPos(textBox); if(c < textBox.value.length) { setCursorPos(textBox,c+1); textBox.curPos = c+1; } // if break; default: // adding a new char somewhere in the field var c = getCursorPos(textBox); var currentMaskChar; // get next available to change.....except masking chars while(c < textBox.value.length) { currentMaskChar = mask.charAt(c); if(currentMaskChar == '9' || currentMaskChar == 'X' || currentMaskChar == 'A') break; c++; } // while switch(currentMaskChar) { case '9': // numeric only if('0123456789'.indexOf(keyCharacter) != -1) addNewKey(textBox,keyCharacter,c); break; case 'A': // alpha only if('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.indexOf(keyCharacter) != -1) addNewKey(textBox,keyCharacter,c); break; case 'X': // alphanumeric if('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.indexOf(keyCharacter) != -1) addNewKey(textBox,keyCharacter,c); break; default: break; } // switch } // switch return retVal; } // -------------------------------------------------------------------- // Accept a TextBox, a keycharacter and an integer position // adds the key to the position and then sets cursor to next availble spot // -------------------------------------------------------------------- function addNewKey(tb,key,pos) { // add the new key to the textbox at pos var startSel = tb.value.substring(0,pos); var endSel = tb.value.substring(pos+1,tb.value.length); tb.value = startSel + key + endSel; // advance cursor to next '_' while(pos < tb.value.length) { curChar = tb.value.charAt(pos); if(curChar == '_') break; pos++; } // while setCursorPos(tb,pos); tb.curPos = pos; } // -------------------------------------------------------------------- // Loops thru pasted value and checks each char against the mask // // Leaves old value and return false if *any* char is off // Creates new masked value if all pasted data is ok // -------------------------------------------------------------------- function tbPaste(textBox) { // grab the textBox value and the mask var pastedVal = window.clipboardData.getData("Text"); var mask = textBox.mask; var newVal = ''; var curPastedVal = 0; for(var i=0;i