练习JavaScript实现过滤特殊字符

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>
posted on 2015-12-31 08:50  土dou  阅读(334)  评论(0编辑  收藏  举报