Javascript验证在Windows和Android手机上不起作用
问题描述:
我已经为名字"实现了Java脚本验证,该名字仅接受字符.它可以在计算机和Iphone上正常工作,但是如果我在其他手机上检查它,它也可以接受数字.我该怎么办?
我正在使用以下代码
在此先感谢
I have implemented java script validation for "first name" which accepts character only. It''s working proper on computer and Iphone but if I a checking It on other mobile It''s accept numbers also. What should I do for this problem?
I am using following code
Thanks In Advance
var alpha = "[ A-Za-z]";
var numeric = "[0-9]";
var alphanumeric = "[ A-Za-z0-9]";
function onKeyValidate(e,charVal){
var keynum;
var keyChars = /[\x00\x08]/;
var validChars = new RegExp(charVal);
if(window.event)
{
keynum = e.keyCode;
}
else if(e.which)
{
keynum = e.which;
}
var keychar = String.fromCharCode(keynum);
if (!validChars.test(keychar) && !keyChars.test(keychar)) {
return false
} else{
return keychar;
}
}
<input type="text" value="" name="first_name" id="first_name" placeholder="First Name" onkeypress="return onKeyValidate(event,alpha);"/>
我尝试过的事情:
我在上面的代码中尝试过此代码,请帮助我
What I have tried:
I tried this above code please help me
答
请参阅我对该问题的评论.
请查看您缺少的API: Event.preventDefault()— Web API | MDN [ ^ ].
另请参见本教程:键盘文本输入过滤:示例 [ Regex教程-Unicode字符和属性 [
Please see my comment to the question.
Please see the API you are missing: Event.preventDefault() — Web APIs | MDN[^].
See also this tutorial: Keyboard Text Input Filtering: Examples[^].
If you want to allow any Unicode letter, not just narrow subset of letters A to Z and a to z, you have to use Unicode Regular Expressions. They are really supported by any non-nonsense JavaScript implementation. Please see: Regex Tutorial — Unicode Characters and Properties[^].—SA