如何验证文本框只接受字符 [英] how to validate textbox to accept only characters

查看:69
本文介绍了如何验证文本框只接受字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...

我正在使用RAD文本框..我想检查一下,如果用户在文本框中输入值,它应该只有字符允许间隔OK。我希望它通过JavaScript并且应该显示应该仅输入字符的用户的消息框所有这些功能都应该在点击提交按钮时发生...

谢谢!!!!!

hi...
I am using RAD Text box..I want to check that if user is entering the value in text box it should only character allow spacing its OK. I want it through JavaScript and should display the message box for user that should" enter character only" all this functionality should happen on click of Submit Button...
thanks!!!!!

推荐答案

<script>
function onkeydown(){
     var ch = String.fromCharCode(event.keyCode);
     var filter = /[a-zA-Z]/   ;
     if(!filter.test(ch)){
          event.returnValue = false;
     }
}
</script>



这可以限制用户只输入字符,但你仍然需要考虑该用户可以粘贴数字,或者拖入文本框。


This can restrict the user to input only characters, but you still have to consider that user can paste numbers, or drag in into textbox.


使用 event.keyCode 只接受char值。

在javascript中编写一个函数并在 onkeypress上调用 Textbox事件



Use event.keyCode to accept only char values.
write a function in javascript and call it on onkeypress event of Textbox

function checkNum()
{

if ((event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 96 && event.keyCode < 123) || event.keyCode == 8)
   return true;
else
   {
       alert("Please enter only char");
       return false;
   }

}


这篇关于如何验证文本框只接受字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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