-1

I am using the following code to make field numeric only. Everything going fine, but it allows more than one decimal point.Experts please help..

$("#" + fieldId).keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190,110]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }

    });
Mak Reza
  • 1
  • 4

3 Answers3

0

try something like (from here)

$('.number').keypress(function(event) {
  if ((event.which != 46 || $(this).val().indexOf('.') != -1) &&
    ((event.which < 48 || event.which > 57) &&
      (event.which != 0 && event.which != 8))) {
    event.preventDefault();
  }

  var text = $(this).val();

  if ((text.indexOf('.') != -1) &&
    (text.substring(text.indexOf('.')).length > 1) &&
    (event.which != 0 && event.which != 8) &&
    ($(this)[0].selectionStart >= text.length - 1)) {
    event.preventDefault();
  }
});
Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0
Number.prototype.countDecimals = function () {
    if(Math.floor(this.valueOf()) === this.valueOf()) return 0;
    return this.toString().split(".")[1].length || 0; 
}

Ex.

var x = 23.453453453;
x.countDecimals(); // 9

check count before event.preventDefault();

This may help you.

$("#" + fieldId).keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190,110]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ($("#" + fieldId).val().countDecimals() >=2  || (e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }

    });
Parth Trivedi
  • 3,802
  • 1
  • 20
  • 40
  • $("#" + fieldId).val().countDecimals() this will give decimal points. If at any key press this value > or equal to 2 then we prevent that key to write. – Parth Trivedi Dec 07 '15 at 12:00
  • Can you make console.log($("#" + fieldId).val().countDecimals() >=2). and check is it getting true/false after 2 decimal points. – Parth Trivedi Dec 07 '15 at 12:01
0

if your happy with your solution and you want just to allow one decimale you will have just add an extra line to check that, I use your code but I change keydown to keypress otherwise it wont work to catch the dot character, I added( ||($(this).val().split(".").length - 1 > 0 && String.fromCharCode(e.which) == '.') ) at the end of the code:

-the added code just check whether there is already a dot and if the pressed key is dot.

$("#" + fieldId).keypress(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190,110]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ($("#" + fieldId).val().countDecimals() >=2  || (e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105 || ($(this).val().split(".").length - 1 > 0 && String.fromCharCode(e.which) == '.'))) {
            e.preventDefault();
        }

    });
  • your way of doening it wont prevent a user from pasting a invalid content you you can prevent user from dat with this code:

      $("#" + fieldId).bind('paste', function (e) 
      {
         e.preventDefault();
      });
    
  • I would advise you not to validate on kepress or keydown because of:

  • user can type a dot after the number and leave the input (like 88. => not valid) which your code wont prevent(and can not otherwise the user can not type a decimale number).
  • bij pressing many keys at the same time I was abel to get invalid characters in the input without preventing them.

-I would advise you to validate on blur so that the user get the freedom of editing and you can be sure of validating the real content of the input.

Khalleo
  • 1
  • 1