注册用户Js验证
function checkform()
{
if(!validateUname('username'))
{
return false;
}
}
//显示错误信息
function displayErrInfo(msg){
var dInfo = document.getElementById("message");
dInfo.innerHTML="";
dInfo.innerHTML=msg;
}
function validateUname(uid){
var username = document.getElementById(uid).value;
if( username == "" || username == null ){
alert( "请先填写用户名!" );
return false;
}
var preString = "<strong>用户名输入规则:</strong>";
switch( isUsername( username ) ){
case 0: break;
case 1: {
displayErrInfo( "用户名不能以数字开头" );
return false;
}
case 2: {
displayErrInfo( "<p>有效用户名长度为6-20个字符</p>" );
return false;
}
case 3: {
displayErrInfo( "<p>用户名只能包含_,英文字母,数字</p>" );
return false;
}
case 4: {
displayErrInfo( "<p>用户名必须以英文字母开头</p>" );
return false;
}
}
return true;
}
</script>
保存为check.js
function trim(s){
return s.replace(/^\s*/,"").replace(/\s*$/,"");
}
function fLen(Obj){
var nCNLenth = 0;
var nLenth = Obj.length;
for (var i=0; i<nLenth; i++){
if(Obj.charCodeAt(i)>255){
nCNLenth += 2;
}else{
nCNLenth++;
}
}
return nCNLenth;
}
function isUsername( username ){
if( /^\d.*$/.test( username ) ){
//用户名不能以数字开头
return 1;
}
if(! /^\w+$/.test( username ) ){
//由数字、26个英文字母或者下划线组成的字符串
return 3;
}
if(! /^([a-z]|[A-Z])[0-9a-zA-Z_]+$/.test( username ) ){
//用户名只能包含_,英文字母,数字
return 4;
}
if(fLen( username )<6 || fLen( username )>20 ){
//合法长度为6-18个字符
return 2;
}
return 0;
}
详细信息验证
<SCRIPT language=JavaScript>
function is_zw(str)
{
exp=/[0-9a-zA-Z_]/g;
if(str.search(exp) != -1)
{
return false;
}
return true;
}
function is_number(str)
{
exp=/[^0-9()-]/g;
if(str.search(exp) != -1)
{
return false;
}
return true;
}
function is_email(str)
{ if((str.indexOf("@")==-1)||(str.indexOf(".")==-1)){
return false;
}
return true;
}
function checkform(){
if(inputform.nickname.value==''){
alert("您没有填写 昵称!");
inputform.nickname.focus();
return false;
}
if(!is_zw(inputform.nickname.value)) {
alert("\昵称必须用中文!")
return false;
}
if( inputform.password.value =="" ) {
alert("\请输入密码 !!")
return false;
}
if( inputform.confirmPassword.value =="" ) {
alert("\请输入密码确认 !!")
return false;
}
if( inputform.confirmPassword.value != inputform.password.value ) {
alert("\两次密码输入不一致 !!")
return false;
}
if( inputform.question.value == "") {
alert("\请输入问题,当您忘记密码时可根据该问题提示密码 !!")
return false;
}
if( inputform.answer.value =="") {
alert("\请输入问题答案 !!")
return false;
}
if( inputform.email.value =="") {
alert("\请输入E-mail !!")
return false;
}
if(!is_number(document.inputform.telephone.value)){
alert("电话必须用数字或-()!");
inputform.telephone.focus();
return false;
}
if(!is_email(inputform.email.value))
{ alert("非法的EMail地址!");
inputform.email.focus();
return false;
}
return true;
}
</SCRIPT>

浙公网安备 33010602011771号