te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>html - Limiting number value in an input tag with javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Limiting number value in an input tag with javascript - Stack Overflow

programmeradmin3浏览0评论

I am creating a form and i want the input tag to accept numbers only and i also want to limit the numbers to be from 0 - 20.

This my html code:

<input type="text" name="three" class="actual" id="three" maxlength="2" onkeypress="return ForNumbers(event)" />

This is my javascript code:

    <script type="text/javascript">
        function ForNumbers(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
return true;
        }
    </script>

I am creating a form and i want the input tag to accept numbers only and i also want to limit the numbers to be from 0 - 20.

This my html code:

<input type="text" name="three" class="actual" id="three" maxlength="2" onkeypress="return ForNumbers(event)" />

This is my javascript code:

    <script type="text/javascript">
        function ForNumbers(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
return true;
        }
    </script>
Share Improve this question edited Nov 21, 2013 at 8:06 zzlalani 24.4k16 gold badges47 silver badges73 bronze badges asked Nov 21, 2013 at 7:56 PeterPeter 331 gold badge1 silver badge4 bronze badges 2
  • what is the issue with this code? any error? or any abnormal behavior?? – zzlalani Commented Nov 21, 2013 at 8:00
  • i need an additional code to it. i want to the input tag accept only values between 0 and 20 – Peter Commented Nov 21, 2013 at 8:12
Add a ment  | 

4 Answers 4

Reset to default 9

Should be able to do something like this:

if (input.value < 0) input.value = 0;
if (input.value > 20) input.value = 20;

Simple as that if that's what you mean.

You could get this to run when the value for the input changes (onChange) that way they can see the limit.

Full function:

HTML:

<input type="text" onchange="limiter(this);" />

Javascript:

function limiter(input) {
   if (input.value < 0) input.value = 0;
   if (input.value > 100) input.value = 100;
}

Working Demo

If you need to support old browsers, try this:

    function ForNumbers(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode;

        if (
        //0~9
        charCode >= 48 && charCode <= 57 ||
           //number pad 0~9
           charCode >= 96 && charCode <= 105 ||
        //backspace
           charCode == 8 ||
        //tab
        charCode == 9 ||
        //enter
        charCode == 13 || 
        //left, right, delete..
        charCode >= 35 && charCode <= 46
        )
        {
        //make sure the new value below 20
        if(parseInt(this.value+String.fromCharCode(charCode), 10) <= 20) 
            return true;
        }

        evt.preventDefault();
        evt.stopPropagation();

        return false;
    }

http://jsfiddle/3StSM/2/

It also works fine with:

  <input id="numIpt" type="number" min="0" max="20" maxlength="2" />

Use html type number and inline elements min="1" and max="20" to limit your input field.

<input type="number" min="1" max="20" />

HTML

1. in input tag add a ID.

<input id="technicalMasterSP" type="text" placeholder="0"c>

2. if you insert value by self created keyboard ( Ex : Calculator, etc.).

<div class="keyboard">
   <div class="button div1" onclick="sendValue('1')">1</div>
   <div class="button div2" onclick="sendValue('2')">2</div>
</div>

JavaScript

3 create a function and check a condition if my #display value length is less than 13 ( 13 is indexing number so 13 = 14 digits ) so add value in my display input box, if my #display length is equal to 13 so don't add any other value.

function sendValue(value) {
   let disValue = document.getElementById('technicalMasterSP')
   if(disValue.value.length <= 13){
      document.getElementById('display').value += value;
   }
}

If you use external keyboard

so add maxlength attribute in your input tag.

<input id="technicalMasterSP" type="text" placeholder="0" maxlength="10">
发布评论

评论列表(0)

  1. 暂无评论