阻止用户在文本框中写信 [英] prevent user from writing letter in textbox

查看:74
本文介绍了阻止用户在文本框中写信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i希望在javascript中创建函数,这可以防止用户在文本框中写字母只允许数字

i找到这一个

  function  prevent_number(event)
{
var charcode =(event.which)? event.which:event.keycode
if (charcode> 31&&(charcode< 48 || charcode> 57))
{


alert(' 请仅输入数字' );
return false ;

}
else
{


return true ;

}





}





i想知道为什么他说charcode> 31&&(charcode< 48 || charcode> 57)

他们的代码从0到9的数字只是从48到57

解决方案

 charcode> 31 



是多余的,以下将做:

 if(charcode< 48 || charcode> 57)


此处的字符代码表示字符的ASCII值。

如果您浏览 Ascii表中的字符表[ ^ ],您将了解为什么只有这些字符范围对您有用

hi all ,
i want to make function in javascript which can prevent user from writing letters in textbox only allow number
i found this one

function prevent_number(event)
   {
       var charcode = (event.which) ? event.which : event.keycode
       if (charcode >31 &&(charcode<48||charcode >57))
        {


            alert('please enter only number ');
            return false;

       }
       else
        {


           return true ;

       }





   }



i want to know why he said charcode>31 &&(charcode<48||charcode>57)
the numbers from 0 to 9 their code is just from 48 to 57

解决方案

charcode >31


is redundant, the following will do:

if (charcode<48 || charcode >57)


Character code here represents the ASCII value of the character.
If you go through the character table at Ascii Table[^], you will get an idea why only these characters ranges are useful to you.


这篇关于阻止用户在文本框中写信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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