在验证域名时要适合的是域名中只能包含字母、数字和"-"字符!并且不能以"-"字符开头,用正则来判断就是/[^a-z0-9-]+/gi; /^-/;re = /-$/;如果是中文域名就re = /^[a-za-z0-9-u4e00-u9fa5ufe30-uffa0]+$/g这样验证
if ( ! fn_checkdomainnameonly ( theelement.value ) )
{
alert ( """ + theelementname + ""中只能包含字母、数字和"-"字符!并且不能以"-"字符开头!" ) ;
theelement.focus ( ) ;
return false ;
}
return true ;
}function fn_checkdomainname ( strdomainname )
{
strdomainname = trimstring ( strdomainname ) ;
arrayofstrings = strdomainname.split(".");
if ( arrayofstrings.length < 2 )
{
return (false); // no enough parts
}
for(i=0;i<arrayofstrings.length;i++)
{
str = trimstring(arrayofstrings[i]);
if(str.length==0)
{
return (false); // empty part
}
re1 = /[^a-z0-9-]+/gi;
match=re1.test(str);
if ( match )
return (false); // non digit and character and '-' char
re1 = /^-/;
match=re1.test(str);
if ( match )
return (false); // start with '-'
re1 = /-$/;
match=re1.test(str);
if ( match )
return (false); // end with '-'
}
return (true);
}
中文验证
function fn_checkcndomainname ( strdomainname )
{
strdomainname = trimstring ( strdomainname ) ;
arrayofstrings = strdomainname.split(".");
if ( arrayofstrings.length < 2 )
{
return (false); // no enough parts
}
for(i=0;i<arrayofstrings.length;i++)
{
str = trimstring(arrayofstrings[i]);
// must not exceed 20 chars
if( str.length==0 || str.length > 20)
{
return (false);
}
// with only leagal chars
re = /^[a-za-z0-9-u4e00-u9fa5ufe30-uffa0]+$/g
if ( ! re.test(str) )
{
return (false);
}
// must with a chinese char at least
// re = /^[a-za-z0-9-]+$/g
// if ( re.test(str) )
// {
// return (false);
// }
// must not start with '-'
re = /^-/;
if ( re.test(str) )
return (false);
// must not end with '-'
re = /-$/;
if ( re.test(str) )
return (false);
}
return (true);
}