donderdag 17 januari 2013

Replace Point keystroke with a Comma


Sometimes you need to prevent your user to use some key characters in your Input Fields.
Like my customer wanted also to use the Comma [,] as the Point [.] as decimal seperator. 
If you use for example Dutch regional settings the decimal seperator is the [,] and the Point is used
as thousant seperator. So my customer wanted to use as Point as Comma as seperator. 
What to do about it? 
Simple answer. Catch the keystroke of the Point and replace it by a Comma. 
My solution below is not perfect if they put a Point between the numbers. 
If someone knows what I have to add in place of the fill up of the ","
please don't hesitate to let me know.
 

$(document).ready(function() {
 if ($(":input[title='MyField']").val() != "")
  $("nobr:contains('MyField')").closest('tr').hide();
 ReplacePointbyComma();
});

function ReplacePointbyComma(){
    $(":input[title='MyNumericField']").keydown(function(e) {
  var keycode = (e.keyCode ? e.keyCode : e.which);
  /// 110 Point on numeric keyblock
  /// 190 Shift Point in alfanumeric keyblock
  if (keycode == 110 || keycode == 190 )
  {
   e.preventDefault();
   $(":input[title='MyNumericField']").val($(":input[title='MyNumericField']").val() + ",");
  }
 });
}
 

Geen opmerkingen:

Een reactie posten