当类型不匹配时,避免在文本框中取值 [英] avoiding taking value in textbox when type be mismatched

查看:57
本文介绍了当类型不匹配时,避免在文本框中取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个输入文本框类型的文本,它包括键盘的所有符号。



Here is a input textbox typed text , it include all symbol of keyboard.

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html>







但我想要的时候我键入除字母之外的任何键,它将不会写在这里.........是可能的吗?



需要帮助....谢谢




But I want when I type any key except alphabet it won't be written here......... is that possible??

need help.... thanks

推荐答案

<input type="text" name="name" onkeydown="return alphaOnly(event);">
</input>


带有javascript函数的



with javascript function

function alphaOnly(event) {
  var key = event.keyCode;
  return ((key >= 65 && key <= 90) || key == 8);
};


也许你可以利用HTML5的'pattern'属性,使用RegEx只允许字母,可能是这样的:< br $>


Maybe you could take advantage of HTML5's 'pattern'attribute, using a RegEx to allow only letters, possibly something like this:

<input type='text' name='name' pattern='[A-Za-z\\s]*'/>


< script type =text / javascript>

function txtAlphabets(e){

var key;

AvoidCopyPaste();

key =(e.which)? e.which:e.keyCode

if((键> 97 &&键< 122)||(键> 65 &&键< 90)||(键> 13 &&键< = 15)||(key == 8)||(key == 0))



e.which;

否则

e.preventDefault();

}











< input type =textname =nameonkeypress =txtAlphabets(event)/>



这将适用于所有浏览器
<script type="text/javascript">
function txtAlphabets(e) {
var key;
AvoidCopyPaste();
key = (e.which) ? e.which : e.keyCode
if ((key > 97 && key < 122) || (key > 65 && key < 90) || (key > 13 && key <= 15) || (key == 8) || (key == 0))

e.which;
else
e.preventDefault();
}





<input type="text" name="name" onkeypress="txtAlphabets(event)/>

This will work in all the browser


这篇关于当类型不匹配时,避免在文本框中取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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