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>