0

below i my code that should allow proper decimal values , but i am unable to use decimal , also can i add regex to validate proper decimal like not allowing two dots

    $("#allowdecimal").keyup(function () {
        var val = $(this).val();
        val = val.replace(/[^\d.]/g, '');
        $(this).val(val);
    });
gaurav
  • 237
  • 4
  • 16
  • Check this [answer](http://stackoverflow.com/a/18085/4519059) ;). – shA.t Oct 03 '16 at 06:19
  • Possible duplicate of [Validate decimal numbers in JavaScript - IsNumeric()](http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) – BadHorsie Oct 03 '16 at 10:35

2 Answers2

0

Just use :

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

val=Number(val);
if(isNaN(val))
    alert("invalid number")
else
    alert(val);
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
0
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#allowdecimal").blur(function () {
 var regexp = /^\d+\.\d{0,2}$/;

 if(regexp.test($(this).val()) == false)
  {
  $(this).val("");
  }
 });
});
</script>
</head>
<body><input type='text' id ='allowdecimal'/>


</body>
</html>
Jobelle
  • 2,717
  • 1
  • 15
  • 26