Saturday, June 25, 2011

Creating a Currency Masked TextBox with On-the-Fly Currency Formatting

 

Referred URL :

http://scottonwriting.net/sowblog/archive/2011/06/25/creating-a-currency-masked-textbox-with-on-the-fly-currency-formatting.aspx?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+ScottOnWriting+(Scott+on+Writing)

Allowing Only Currency-Related Characters In a TextBox

There are a number of existing masked input plugins for jQuery. After trying some out I decided to roll my own JavaScript functions. I intend to come back to these and turn them into jQuery plugins, but for now they’re just JavaScript functions. As you can see in the script below, I created four functions:

  • numbersOnly – allows just number inputs, whether they are from the letters at the top of the keyboard or from the number pad.
  • numbersAndCommasOnly – allows number inputs and commas.
  • decimalsOnly – allows numbers, commas, and periods (either from the main keyboard or the number pad).
  • currenciesOnly – allows numbers, commas, periods, and the dollar sign.

In addition to the allowed characters discussed above, the functions also permit “special character key codes,” namely Delete, Backspace, left arrow, right arrow, Home, End and Tab. What keycodes are valid are listed in the variables at the top of the script; see Javascript Char Codes for a table listing the keys and their corresponding key codes.

Here is the script of interest:

// JavaScript I wrote to limit what types of input are allowed to be keyed into a textbox 
var allowedSpecialCharKeyCodes = [46,8,37,39,35,36,9];
var numberKeyCodes = [44, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105];
var commaKeyCode = [188];
var decimalKeyCode = [190,110];

function numbersOnly(event) {
var legalKeyCode =
(!event.shiftKey && !event.ctrlKey && !event.altKey)
&&
(jQuery.inArray(event.keyCode, allowedSpecialCharKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, numberKeyCodes) >= 0);

if (legalKeyCode === false)
event.preventDefault();
}

function numbersAndCommasOnly(event) {
var legalKeyCode =
(!event.shiftKey && !event.ctrlKey && !event.altKey)
&&
(jQuery.inArray(event.keyCode, allowedSpecialCharKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, numberKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, commaKeyCode) >= 0);

if (legalKeyCode === false)
event.preventDefault();
}

function decimalsOnly(event) {
var legalKeyCode =
(!event.shiftKey && !event.ctrlKey && !event.altKey)
&&
(jQuery.inArray(event.keyCode, allowedSpecialCharKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, numberKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, commaKeyCode) >= 0
||
jQuery.inArray(event.keyCode, decimalKeyCode) >= 0);

if (legalKeyCode === false)
event.preventDefault();
}

function currenciesOnly(event) {
var legalKeyCode =
(!event.shiftKey && !event.ctrlKey && !event.altKey)
&&
(jQuery.inArray(event.keyCode, allowedSpecialCharKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, numberKeyCodes) >= 0
||
jQuery.inArray(event.keyCode, commaKeyCode) >= 0
||
jQuery.inArray(event.keyCode, decimalKeyCode) >= 0);

// Allow for $
if (!legalKeyCode && event.shiftKey && event.keyCode == 52)
legalKeyCode = true;

if (legalKeyCode === false)
event.preventDefault();
}

My script is, admittedly, very US-centric. I have not tested the key codes with a non-English keyboard and for currencies I only allow a dollar sign. For the project I wrote this script for this is (currently) a non-issue since it is used within the corporate firewall and all sales are domestic, but clearly the above script would not work as well for international settings.

Applying the Currency Masking Script to a TextBox on the Page


With the above script in place you can have a textbox on the page mask its input by having the appropriate function called in response to the keydown event. The following jQuery syntax wires up this logic to all single-line textboxes that have the CSS class currenciesOnly.

$(document).ready(function () {
$("input[type=text].currenciesOnly").live('keydown', currenciesOnly);
});

That’s it!

Formatting the Just-Entered Currency


Another requirement my client had was to format the just-entered number as a currency. That is, to change the user’s input – say, 45000 – to a formatted value like $45,000.00 immediately after their tabbed out of the textbox. To accomplish this I used Ben Dewey’s jQuery Format Currency Plugin, which you can see a demo of athttp://www.bendewey.com/code/formatcurrency/demo/. This plugin adds a formatCurrency function that you can call on a set of elements returned by a jQuery selector.

To use this plugin I updated the $(document).ready event handler shown above to also call theformatCurrency function on blur:

$(document).ready(function () {
$("input[type=text].currenciesOnly").live('keydown', currenciesOnly)
.live('blur',
function () {
$(this).formatCurrency();
}
);
});

In short, whenever a textbox with a CSS class of currenciesOnly is blurred, the just-blurred textbox’s inputs are formatted as a currency thanks to the formatCurrency function.

No comments: