How to check input of decimal value?

Currently, only decimal values ​​work, in which the user enters only the decimal value in the text field. If you accept only 10 numbers, for example, if the user enters 12345.000000, he should receive a warning: Field field format error. Repeat entry using the correct format. (6.3)

With my current jquery code, it will take a decimal value

$("#txtQty").keyup(function() {
var $this = $(this);
$this.val($this.val().replace(/[^\d.]/g, ''));        
});

This is my html code for a text field with this html, it will only allow 10 characters

<input id="txtQty" type="text" maxlength="10"/>

I'm tired of the bottom SO user, but still I get the same problem

   $('#txtQty').focusout(function(e) {
   if('123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null){
      alert("Number field format error. Please re-enter using the proper format. (6.3)");  
   }else{
       '123456.7890'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
   }
});

(1-6) 6 , 3 , , , ,

123456.000 - true
12345.000 -
1234.000 -
123.000 - true
12.000 - true
1.000 - true

$('.ipt_Havg').focusout(function (e) {
var regexp = /\d*\.\d{3}/
if (document.getElementById("ipt_Havg").value !== regexp) {
    alert("Number field format error. Please re-enter using the proper format. (6.3)");
} else {
    alert('nothing wrong here');
}
});
+4
4

RegExps:

'123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
true
'123456.7890'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null
false
-1

1) if/else ...

if('123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null){
   alert("Number field format error. Please re-enter using the proper format. (6.3)");  
}else{
   '123456.7890'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null // <- comparison makes no sense here
}

"" else. else "" (- ), .

: MDN "if... else"

- , -, - .

if ('123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) !== null) {
   alert("Number field format error. Please re-enter using the proper format. (6.3)");  
} else {
   alert('nothing wrong here');
}

2) . , , , '123456.789' . , , ...

'123456.789'.match(/^[0-9]{6}\.[0-9]{3}$/) true, '123456.789' - , .

...

$('#ipt_Havg').val().match(/\d*\.\d{3}/) !== null

3) .

if ($('#ipt_Havg').val().match(/\d*\.\d{3}/) !== null) {
    alert('nothing wrong here');
} else {
    alert("Number field format error. Please re-enter using the proper format. (6.3)");
}

DEMO: http://jsfiddle.net/wfzv0h5y/


4) ...

, , , tht, , 1234.0000,

...

^\d{1,6}\.0{3}$

DEMO: http://jsfiddle.net/wfzv0h5y/6/

+3

JQuery Number Formatter Plugin - .

https://github.com/andrewgp/jsNumberFormatter

, .

0

HTML5:

<input type='number' step='0.001' max="999999.999" />

step= , max= .

0

Source: https://habr.com/ru/post/1616056/


All Articles