限制输入类型编号中的字符数 [英] Limit number of characters in input type number

查看:118
本文介绍了限制输入类型编号中的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制输入中的字符数(数字类型)。香港专业教育学院尝试了很多选择,似乎没有任何工作。我不想使用选项电话,因为它需要移动设备上的数字键盘(是的,与。和所有的符号)我也试图模式解决方案,但它只适用于iOS,没有工作在Android(显示正常键盘)。

最好的办法是,如果用户达到限制,不要让他再输入,如果他想突出显示文本并重新键入不同的数字,他是允许的至。只是不要让他输入超过指定数量的字符。



所以,任何帮助表示赞赏。

解决方案

检查此代码

JavaScript


$ b

 < script> 
函数检查(e,值)
{
//检查字符
var unicode = e.charCode? e.charCode:e.keyCode;
if(value.indexOf(。)!= -1)if(unicode == 46)return false; (unicode!= 8)if((unicode< 48 || unicode> 57)&& unicode!= 46)返回false;
if(unicode!
}
函数checkLength()
{
var fieldLength = document.getElementById('txtF').value.length;
//假设你想要4个字符
if(fieldLength <= 4){
return true;
}
else
{
var str = document.getElementById('txtF')。value;
str = str.substring(0,str.length - 1);
document.getElementById('txtF')。value = str;
}
}



和HTML 输入数字类型在下面

  onInput //如果在Chrome浏览器中从字段的侧向箭头改变值,也会触发

< input id =txtFtype =number onKeyPress =return check(event,value)onInput =checkLength()/>

Fiddle Demo



更新 - 一点通用代码示例

将上面的函数转换成这个函数

pre $函数checkLength(len,ele){
var fieldLength = ele.value.length;
if(fieldLength <= len){
return true;
}
else
{
var str = ele.value;
str = str.substring(0,str.length - 1);
ele.value = str;






在HTML中使用像这样

 <! -  length 4  - > 
< input id =txtFtype =numberonKeyPress =return check(event,value)onInput =checkLength(4,this)/>
<! - 长度5 - >
< input type =numberonKeyPress =return check(event,value)onInput =checkLength(5,this)/>
<! - 长度2 - >
< input type =numberonKeyPress =return check(event,value)onInput =checkLength(2,this)/>

演示

Im trying to limit to X number the characters in a input (type of number). ive tried a lot of options and none seems to work. I dont want to use the option tel as it needs the numeric keyboard on a mobile device (yes, with ., and all the symbols) I tried also the pattern solution but it only worked for iOS, didnt work in android (displayed the normal keyboard).

The best way would be that if the user hits the limit dont let him type anymore, if he wants to highlight the text and re-type a different number he is allow to. Just not let him type more than the specified number of characters.

So, any help is appreciated.

解决方案

Check this code

JavaScript

<script>
function check(e,value)
{
    //Check Charater
    var unicode=e.charCode? e.charCode : e.keyCode;
    if (value.indexOf(".") != -1)if( unicode == 46 )return false;
    if (unicode!=8)if((unicode<48||unicode>57)&&unicode!=46)return false;
}
function checkLength()
{
    var fieldLength = document.getElementById('txtF').value.length;
    //Suppose u want 4 number of character
    if(fieldLength <= 4){
        return true;
    }
    else
    {
        var str = document.getElementById('txtF').value;
        str = str.substring(0, str.length - 1);
        document.getElementById('txtF').value = str;
    }
}

and HTML input with number type below

onInput //Is fired also if value change from the side arrows of field in Chrome browser

<input id="txtF" type="number" onKeyPress="return check(event,value)" onInput="checkLength()" />

Fiddle Demo

Update -- Little bit generic code example

Change above function into this one

function checkLength(len,ele){
  var fieldLength = ele.value.length;
  if(fieldLength <= len){
    return true;
  }
  else
  {
    var str = ele.value;
    str = str.substring(0, str.length - 1);
    ele.value = str;
  }
}

In HTML use like this

<!-- length 4 -->    
<input id="txtF" type="number" onKeyPress="return check(event,value)" onInput="checkLength(4,this)" />
<!-- length 5 -->    
<input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(5,this)" />
<!-- length 2 -->    
<input  type="number" onKeyPress="return check(event,value)" onInput="checkLength(2,this)" />

Demo

这篇关于限制输入类型编号中的字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆