如何用javascript判断字符串只能是数字或字母组合

来源:百度知道 编辑:UC知道 时间:2024/05/12 17:23:29
判断一个字符串只能是数字或字母.即要么全是数字,要么全是字母,要么是有字母和数字组合

<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) || cChec