当前位置: 代码迷 >> HTML/CSS >> 文本框限制字符长度,该怎么解决
  详细解决方案

文本框限制字符长度,该怎么解决

热度:307   发布时间:2012-02-25 10:01:47.0
文本框限制字符长度
<INPUT onkeydown=if(event.keyCode==13)event.keyCode=9 onkeyup="value=value.replace(/[^0-9- ]/g,'');" maxLength=11 >

上面这样写只能限制输入的一定是数字 和最大长度为11个字符 如何限制一定要输入11位 不能少也不能多

------解决方案--------------------
探讨

引用:
你确定上面的写法对吗。。。测试怎么通不过呢。。



不对吗 测试那里不对 这个现在只是限制输入一定是数字和11个字符

------解决方案--------------------
探讨
<INPUT onkeydown=if(event.keyCode==13)event.keyCode=9 onkeyup="value=value.replace(/[^0-9- ]/g,'');" maxLength=11 >

上面这样写只能限制输入的一定是数字 和最大长度为11个字符 如何限制一定要输入11位 不能少也不能多

------解决方案--------------------
探讨

引用:
引用:
<INPUT onkeydown=if(event.keyCode==13)event.keyCode=9 onkeyup="value=value.replace(/[^0-9- ]/g,'');" maxLength=11 >

上面这样写只能限制输入的一定是数字 和最大长度为11个字符 如何限制……

------解决方案--------------------
if(document.form.mobile.value=="")
{
alert("您输入错误");
document.forma.mobile.focus();
return false;
}
else
{
if(!/^\d{11}$.test(document.form.mobile.value))
{alert("您输入数字的位数不对");
document.forma.mobile.focus();
return false;
}
}
不知道这样行不行。。。
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>控制textarea的字符个数</title>
<style>
<!--
form{
    padding:0px;
    margin:0px;
    font:14px Arial;
}
input.txt{                        /* 文本框单独设置 */
    border: 1px inset #00008B;
    background-color: #ADD8E6;
}
input.btn{                        /* 按钮单独设置 */
    color: #00008B;
    background-color: #ADD8E6;
    border: 1px outset #00008B;
    padding: 1px 2px 1px 2px;
}
-->
</style>
<script language="javascript">
function LessThan(oTextArea){
    //返回文本框字符个数是否符号要求的boolean值
    return oTextArea.value.length < oTextArea.getAttribute("maxlength");
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="addInfo.aspx">
<p><label for="name">请输入您的姓名:</label>
<input type="text" name="name" id="name" class="txt" value="姓名" maxlength="10"></p>
<p><label for="comments">我要留言:</label><br>

<textarea name="comments" id="comments" cols="40" rows="4" maxlength="50" onkeypress="return LessThan(this);"></textarea></p>
<p><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
<input type="reset" name="btnReset" id="btnReset" value="Reset" class="btn"></p>
</form>
</body>
</html>

------解决方案--------------------
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title>电话号码检测 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="Dsmart">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
  电话号码:<input id ="phone" type="value" onblur="checkPhone()"/>
  <script type="text/javascript">
  function checkPhone(){
  var phone = document.getElementById("phone").value;
  if(phone == ""){
  alert("请输入号码");
  }else if(!(/^\d{11}$/g.test(phone))){//限制输入11整数
  //}else if(!(/^13\d{9}$/g.test(phone)||(/^15[0-35-9]\d{8}$/g.test(phone))|| (/^18[05-9]\d{8}$/g.test(phone)))){
             //用于检测用户输入的手机号码是否正确 验证13系列和150-159(154除外)、180、185、186、187、188、189几种号码,长度11位
  alert("请输入11整数");
  }else{
  alert("ok");
  }
  
  }
  </script>
 </body>
</html>
 
  相关解决方案