jump to navigation

Currency TextBox with Javascript April 24, 2009

Posted by codinglifestyle in Javascript.
Tags: , , , ,
1 comment so far

This took me a long time so here it is for prosperity.  I started by looking at Ajax’s masked edit field in order to enforce a simply currency field.  This was a waste of time and not up for the job.  I accomplished it with javascript instead.  Very simple, no symbols, just #####.## with the cents being optional. 

Here is the script:


**  Returns the caret (cursor) position of the specified text field.

**  Return value range is 0-oField.length.


function doGetCaretPosition(oField) {


    // Initialize

    var iCaretPos = 0;


    // IE Support

    if (document.selection) {


        // Set focus on the element



        // To get cursor position, get empty selection range

        var oSel = document.selection.createRange();


        // Move selection start to 0 position

        oSel.moveStart(‘character’, -oField.value.length);


        // The caret position is selection length

        iCaretPos = oSel.text.length;



    // Firefox support

    else if (oField.selectionStart || oField.selectionStart == ‘0’)

        iCaretPos = oField.selectionStart;


    // Return results

    return (iCaretPos);



function fnCurrencyOnly(o) {

    var sValue = o.value;

    var sKey = String.fromCharCode(window.event.keyCode);

    var range = document.selection.createRange();

    var pos = doGetCaretPosition(o);


    if (range.text == sValue) {

        sValue = sKey;

    } else {

        sValue = sValue.substr(0, pos) + sKey + sValue.substr(pos);


    var re = new RegExp(“^\\d+(?:(\\.|\\,)\\d{0,2})?$”);

    if (!sValue.match(re))

        window.event.returnValue = false;




        <input type=”text” onkeypress=”fnCurrencyOnly(this);” />



I’m sure sure who the original author is, but I found the script for doGetCaretPosition() here: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/



The correct way to test if a string is a Guid May 25, 2006

Posted by codinglifestyle in C#.
Tags: , , ,
add a comment

Recently I’ve had a few scenarios where I needed to test if a string was a guid.  I had a dirty solution using a try/catch but today I needed to perform this test thousands of times and this was much too slow.  So a quick search turned up this beauty and shows the “right” way to perform this test.

 using System.Text.RegularExpressions;
 private static Regex isGuid = new Regex(@”^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$”, RegexOptions.Compiled);
    internal static bool IsGuid(string candidate, out Guid output)
        bool isValid = false;
        output = Guid.Empty;

        if (candidate != null)
            if (isGuid.IsMatch(candidate))
                output = new Guid(candidate);
                isValid = true;
        return isValid;

Numeric input only for a HtmlInputField January 12, 2006

Posted by codinglifestyle in ASP.NET, Javascript.
Tags: , ,
1 comment so far

Sometimes a validator control is overkill, especially when you are dynamically creating all your web controls and just want a simple restriction.  Below is a little javascript to require numeric input only:

HtmlInputField myField = new HtmlInputField();

myField.Attributes.Add(“onkeyup”, “this.value=this.value.replace(/[^\\d]*/gi,\”\”);”);