首页 > 语言 > PHP > 正文

验证用户输入数字是否在指定的区域

2024-09-04 11:47:33
字体:大 中 小
来源:转载
供稿:网友

js验证,代码如下:

  1. <script type="text/javascript">  
  2. var dnum=document.getElementById("fenshu");  
  3. dnum.onkeyup = function(){  
  4. this.value=this.value.replace(/D/g,'');  
  5. if(dnum.value>1000||dnum.value<100){  
  6.          dnum.value=100;  
  7. } 
  8. } 
  9. </script>  

php验证,代码如下:

  1. $var=300; 
  2.  
  3. $int_options = array( 
  4. "options"=>array 
  5. ( 
  6. "min_range"=>0, 
  7. "max_range"=>256 
  8. ) 
  9. ); 
  10.  
  11. if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) 
  12. { 
  13. echo("Integer is not valid"); 
  14. } 
  15. else 
  16. {//开源代码Vevb.com 
  17. echo("Integer is valid"); 
  18. } 

方法三js

var reg = /d{2,3}/;
var s = 888;
alert(reg.test(s));

php正则:^([1-9][0-9][0-9]|1000)$

这里我们主要是用到了正则表达式来完成,验证数字是否在指定的数据间距之间.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表