如何在javascript中验证字母数字 [英] how to validate alphanumeric in javascript

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

问题描述

我有一个文本框,不需要用户输入任何可以输入的特殊字符

1. A-Z
2. a-z
3. 0-9
4. Space.

I have a textbox and it need the user not allow to enter any specialcharecters He can enter

1. A-Z
2. a-z
3. 0-9
4. Space.

推荐答案

Google!使用从问题主题中复制的 进行的非常简单的搜索就获得了130万次匹配,其中大部分都准确地解释了如何实现.

请,请至少将来对您的自我进行基础研究...
Google! A very simple search using "validate alphanumeric in javascript" copied from your question subject gave 1.3 million hits, most of then explaining exactly how to do it.

Please, try to do at least basic research your self in future...


是时候学习Javascript和正则表达式了(在业余时间,请看一下常见问题解答).
It is time to learn both Javascript and regular expressions (and, in the spare time, have a look at the Q&A FAQ).


请尝试以下操作:

try this:

<script type = "text/javascript">
function checkField(fieldname)
{
if (/[^0-9a-bA-B\s]/gi.test(fieldname.value))
{
alert ("Only alphanumeric characters and spaces are valid in this field");
fieldname.value = "";
fieldname.focus();
return false;
}
}
</script>



并这样称呼:



and call it like this:

<input type="text" name="name" id="name"  onblur="checkField(this)">


这篇关于如何在javascript中验证字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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