Use JavaScript to format number with comma separator for thousands as you type

The other day a client asked me if it was possible to format a number using comma as a thousand separator as they typed into a text box. After doing a little bit of research and playing around, I came up with this solution.

Solution consist of two JavaScript functions – CheckNumeric() to check if the key pressed is a valid numeric value, and FormatCurrency() which formats the value in the text box. CheckNumeric() is bound to onkeypress and FormatCurrency() is bound to onkeyup events on the target textbox.


function FormatCurrency(ctrl) {
    //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
    if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40)
    {
        return;
    }

    var val = ctrl.value;

    val = val.replace(/,/g, "")
    ctrl.value = "";
    val += '';
    x = val.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';

    var rgx = /(\d+)(\d{3})/;

    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }

    ctrl.value = x1 + x2;
}

function CheckNumeric() {
    return event.keyCode >= 48 && event.keyCode <= 57;
}


onkeypress="return CheckNumeric();" onkeyup="FormatCurrency(this);"

Final result:

Advertisements

iOS 8 – How to remove recent contacts from App Switcher

One of iOS 8 features is to show a list most recent contacts in App Switcher. Some users may find this to be convenient, but others may not like this feature.

If you want to remove recent contacts from App Switcher, open Settings, and then go to Mail, Contacts, Calendar, and change Show In App Switcher setting to Off.

That’s it. From now on, App Switcher won’t show recent contact list. To display recent contacts again, just change the setting to On.

iOS Settings

ASP.Net – How to set value of date textbox via server side code

If you are using textbox with TextMode=”Date”, you need to format the date string before setting value via server side code.

Use ToString(“yyyy-MM-dd”) to format the date value before assigning it to the text box.

txtEOIStart.Text = DateTime.Now.ToString("yyyy-MM-dd");

How to reseed identity column using TSQL

If you need to reseed identity column, use DBCC CHECKIDENT command.

DBCC CHECKIDENT (table_name, RESEED, starting_value)

Thing to note here is that starting value should be one less than the next value you want to see in your identity column. So if you are reseeding the table and want to see 1 in the identity column when next record is inserted, put 0 as starting value.

You can read more about DBCC CHECKIDENT on MSDN.

ASP.Net CheckBoxList client side JavaScript validation

If you need to validate CheckBoxList on client side using JavaScript, here is a way to do it. This code will check that at least one checkbox is selected, return true if it is and false if there are no selections.

function Validate() {
     //Other control validation goes here...
     
     var buSelected = false;
     var relevantBUs = document.getElementById("cblBusinessUnits");

     var businessUnit = relevantBUs.getElementsByTagName("input");
     for (var i = 0; i < businessUnit.length; i++) {
          if (businessUnit[i].checked) {
               buSelected = true;
               return;
          }
     }

     if (!buSelected) {
          alert("Please select at least one option.");
          return false;
     }
            
     return true;
}

.Net – How to print directly to printer in Winforms application

Recently I was writing a simple Winforms app in C#, which required printing of labels. Printing had to be automatic, without displaying print dialog box, or requiring any extra user input.

First, include System.Drawing.Printing namespace. Then create a PrintDocument object and attach print page event handler to it and add code inside the handler to create content that gets sent to the printer. Finally, call Print() method on PrintDocument object to send contents directly to printer, without displaying print dialog box.

using System.Drawing.Printing;

private void MyProc()
{
     //Do other stuff before printing...
     //...
     //...

     PrintDocument pd = new PrintDocument();
     pd.PrintPage += new PrintPageEventHandler(PrintLabel);
     pd.Print();
}

private void PrintLabel(object o, PrintPageEventArgs e)
{
     string label = "Text that I want to print goes here.";
     System.Drawing.Font font = new System.Drawing.Font(FontFamily.GenericSerif, 20, FontStyle.Regular);
     System.Drawing.Brush brush = new SolidBrush(Color.Black);
     e.Graphics.DrawString(label, font, brush, 10, 10, StringFormat.GenericDefault);
}

How to delete game from Steam

If you are using Steam to purchase and play your games, and you wish to delete a particular game to free up hard disk space, here is how to do it.

Open Steam client, and go to your Library. Right click on the game you wish to remove, and select “Delete local content”. This will remove game files from your machine. Note that you’ll still see the game in your library (since you’ve already purchased it), but if you wish to play it again, you’ll need to download it and install it again.

Steam