javascript - How to Validate Input value at two decimal point by inputting from number pad? - Stack Overflow

I have a JavaScript function to Validate only decimal value.I use the JavaScript onkeyup event.function

I have a JavaScript function to Validate only decimal value.I use the JavaScript onkeyup event.

function onlyNumeric(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  var exclusions = [8, 46];

  if (exclusions.indexOf(key) > -1) {
    return;
  }

  key = String.fromCharCode(key);

  var regex = /[0-9]|\./;

  if (!regex.test(key)) {
    theEvent.returnValue = false;

    if (theEvent.preventDefault) {
      theEvent.preventDefault();
    }

    return false;
  }

  return true;
}
<input type="text" onkeyup="onlyNumeric(event)" class="form-control" id="ColumnName" name="ColumnName">

I have a JavaScript function to Validate only decimal value.I use the JavaScript onkeyup event.

function onlyNumeric(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  var exclusions = [8, 46];

  if (exclusions.indexOf(key) > -1) {
    return;
  }

  key = String.fromCharCode(key);

  var regex = /[0-9]|\./;

  if (!regex.test(key)) {
    theEvent.returnValue = false;

    if (theEvent.preventDefault) {
      theEvent.preventDefault();
    }

    return false;
  }

  return true;
}
<input type="text" onkeyup="onlyNumeric(event)" class="form-control" id="ColumnName" name="ColumnName">

It's working fine when I put value not from the number pad. But when I put value by number pad it's not working. So how can i fixed it?

Also I need fix it at two decimal point. But how can I do it?

Is onkeyup event Ok for this purpose?

Share Improve this question edited Aug 23, 2019 at 15:00 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 23, 2017 at 8:01 AshiquzzamanAshiquzzaman 5,2944 gold badges30 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Below is the code to help you out:

function onlyNumeric(evt) {
    var pattern = /^\d{0,4}(\.\d{0,2})?$/i;
    var element = document.getElementById("ColumnName");
    if(pattern.test(element.value)) {
      console.log("Vadid number:" + element.value);
    } else {
      console.log("Invadid number:" + element.value);
    }
}
<input type="text" onkeyup="onlyNumeric(event)" class="form-control" id="ColumnName" name="ColumnName">

You can use this Regex pattern :

/^\d+(?:\.\d{1,2})?$/ 

Explanation :

\d    match a digit...   
+     one or more times    
(     begin group...     
?:    but do not capture anything     
\.    match literal dot     
\d    match a digit...     
{1,2} one or two times    
)     end group   
?     make the entire group optional 

<input type="text" onkeyup="onlyNumeric(event)" class="form-control" id="ColumnName" name="ColumnName">

<script>
function onlyNumeric(evt) {
    var rx = /^\d+(?:\.\d{1,2})?$/;
    var ele = document.getElementById("ColumnName");
    if(rx.test(ele.value)) { 
       console.log("true");
    }else{ 
       console.log("false");
    }
}
</script>

Another solution using addEventListener() instead of onkeyup:

<input type="text" class="form-control" id="ColumnName" name="ColumnName">

<script>
function onlyNumeric(evt) {
    var rx = /^\d+(?:\.\d{1,2})?$/;
    var ele = document.getElementById("ColumnName");
    if(rx.test(ele.value)) { 
       console.log("true");
    }else{ 
       console.log("false");
    }
}

var el = document.getElementById("ColumnName");
el.addEventListener("input", onlyNumeric, false);

</script>

Use this jquery plugin it validate decimal behaviour while typing and mouse click https://github./sarkfoundation/Decimal-Behavior

   <script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type='text/javascript' src="https://rawgit./sarkfoundation/Decimal-Behavior/master/sark-decimal/sark-decimal.js"></script>
    <input type="text" data-behaviour="decimal">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745432937a4627475.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信