码迷,mamicode.com
首页 > 编程语言 > 详细

练习JavaScript实现过滤特殊字符

时间:2015-04-27 19:44:29      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

for循环遍历特殊字符和用户输入字符,然后做比较,返回结果。

<script type="text/javascript">
var name = prompt("请输入用户名");
function ifname(value)
{
    var astr = ["~","!","@","#","$","%","^","&","*","(",")","_","+","|",".",",","?"];
    for (var s = 0;s < value.length; s++ )
    {
        for (var d = 0;d <astr.length; d++ )
        {
            if (value.charAt(s) == astr[d])
            {
                return true;
            }
        }
    }
    return false;
}

if (ifname(name))
{
    document.write("不合法");
}else
 {
    document.write("合法");
 }
</script>

 

练习JavaScript实现过滤特殊字符

标签:

原文地址:http://www.cnblogs.com/blogg/p/4461009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!