Wednesday, January 9, 2013

Keypress event in textbox return number formatted by JQuery


It is worked nicely.
$(document).ready(function () {
   $('#<%= TextBoxID.ClientID %>').keyup(function (e) {            
   $(this).val(addCommasOnKeyPress($('#<%= TextBoxID.ClientID %>').val()));  });

});


function addCommasOnKeyPress(nStr) {
    nStr = nStr.replace(/\,/g, '')
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
If Problem happens then it will be for PostBack Events
Just Add this below on vb/cs page:
TextBoxID.Attributes.Add("onKeyUp", "javascript:onkeyupmethod()"); 
and add this under script tag:
function onkeyupmethod()
{ 
  $('#<%= TextBoxID
.ClientID %>').val(addCommasOnKeyPress($('#<%= TextBoxID.ClientID %>').val())); }

No comments:

Post a Comment