如何用javascript判断字符串只能是数字或字母组合
1个回答
展开全部
<script language="javascript">
function IsDigit(cCheck) { return (('0'<=cCheck) && (cCheck<='9')); }
function IsAlpha(cCheck) { return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) }
function VerifyInput()
{
strUserID = document.form1.username.value;
if (strUserID == "")
{
alert("请输入您的用户名");
document.form1.username.focus();
return false;
}
for (nIndex=0; nIndex<strUserID.length; nIndex++)
{
cCheck = strUserID.charAt(nIndex);
if ( nIndex==0 && ( cCheck =='-' || cCheck =='_') )
{
alert("用户名首字符必须为字母或数字");
document.form1.username.focus();
return false;
}
if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' ))
{
alert("用户名只能使用英文字母、数字以及-和_,并且首字符必须为字母或数字");
document.form1.username.focus();
return false;
}
}
return true;
}
</script>
function IsDigit(cCheck) { return (('0'<=cCheck) && (cCheck<='9')); }
function IsAlpha(cCheck) { return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) }
function VerifyInput()
{
strUserID = document.form1.username.value;
if (strUserID == "")
{
alert("请输入您的用户名");
document.form1.username.focus();
return false;
}
for (nIndex=0; nIndex<strUserID.length; nIndex++)
{
cCheck = strUserID.charAt(nIndex);
if ( nIndex==0 && ( cCheck =='-' || cCheck =='_') )
{
alert("用户名首字符必须为字母或数字");
document.form1.username.focus();
return false;
}
if (!(IsDigit(cCheck) || IsAlpha(cCheck) || cCheck=='-' || cCheck=='_' ))
{
alert("用户名只能使用英文字母、数字以及-和_,并且首字符必须为字母或数字");
document.form1.username.focus();
return false;
}
}
return true;
}
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询