Home  >  Article  >  Web Front-end  >  The input box limits the input value to floating point code sharing

The input box limits the input value to floating point code sharing

小云云
小云云Original
2018-02-02 11:53:181417browse

This article mainly brings you a js code that limits the input value of the input box to floating point. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

In some projects, for example, the amount uses floating point type. For input restrictions, please refer to the following:

75ee59d57a751505d76e8d68aa9cb9be


##

<script>
  function only_num(obj){
    //得到第一个字符是否为负号
    var num = obj.value.charAt(0);
    //先把非数字的都替换掉,除了数字和.
    obj.value = obj.value.replace(/[^\d\.]/g,&#39;&#39;);
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,&#39;&#39;);
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,&#39;.&#39;);
    //保证.只出现一次,而不能出现两次以上
    obj.value = obj.value.replace(&#39;.&#39;,&#39;$#$&#39;).replace(/\./g,&#39;&#39;).replace(&#39;$#$&#39;,&#39;.&#39;);
    //如果第一位是负号,则允许添加
    if(num == &#39;-&#39;){
      obj.value = &#39;-&#39;+obj.value;
    }
  }
</script>

Related recommendations:


js Get input tag The input value implementation code_javascript skills

javascript method to limit the input value type of the text box_javascript skills

javascript - How to give in thinkphp Is the parameter passed by the a tag the input value of the text box?


The above is the detailed content of The input box limits the input value to floating point code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn