⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validate.js

📁 一个由B/S做的宠物医院
💻 JS
字号:
//验证用户名.
function validateusername()
{
  var uname=document.frm1.username.value;
  var str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var ch1,ch2;
  ch1=uname.charAt(0);                    //得到uname的首字符.
  ch2=uname.charAt(uname.length-1);       //得到uname的尾字符.
  //判断username文本框的格式。(不能为空、不能小于3位、必须以字母或数字开头或结尾<indexOf()方法的结果为-1表示找不到要找的字符>)
  if(uname=="" || uname.length<3 || str.indexOf(ch1,0)==-1 || str.indexOf(ch2,0)==-1 || !isSsnString(uname))
  {
    document.getElementById("Layer1").style.visibility="visible";
    return false;
  }
  else
  {
    document.getElementById("Layer1").style.visibility="hidden";
    return true;
  }
}
//验证密码.
function validatepassword()
{
  var pwd=document.frm1.password.value;
  if(pwd=="" || pwd.length<6)
  {
    document.getElementById("Layer2").style.visibility="visible";
    return false;
  }
  else
  {
    document.getElementById("Layer2").style.visibility="hidden";
    return true;
  }
}
//验证密码确认.
function validateconfirmpassword()
{
  var pwd=document.frm1.password.value
  var cpwd=document.frm1.confirmPassword.value;
  if(cpwd=="" || cpwd!=pwd)
  {
    document.getElementById("Layer3").style.visibility="visible";
    return false;
  }
  else
  {
    document.getElementById("Layer3").style.visibility="hidden";
    return true;
  }
}
//验证密码提示问题.
function validatequestion()
{
  var question=document.frm1.question.value
  if(question=="")
  {
    document.getElementById("Layer4").style.visibility="visible";
    return false;
  }
  else
  {
    document.getElementById("Layer4").style.visibility="hidden";
    return true;
  }
}
//验证密码提示答案.
function validateanswer()
{
  var answer=document.frm1.answer.value;
  if(answer=="" || answer.length<6)
  {
    document.getElementById("Layer5").style.visibility="visible";
    return false;
  }
  else
  {
    document.getElementById("Layer5").style.visibility="hidden";
    return true;
  }
}
//验证出生年份.
function validateYear()
{
	var year=document.frm1.year.value;
	if(year.length!=4 || isNumberString(year,"1234567890")!=1 || year<1902 || year>2006)
	{
		document.getElementById("Layer6").style.visibility="visible";
		return false;
	}
	else
	{
		document.getElementById("Layer6").style.visibility="hidden";
    		return true;
	}
}
//验证出生日期.
function validateDate()
{
	var year=document.frm1.year.value;
	var month=document.frm1.month.value;
	var day=document.frm1.day.value;
	if (((Math.abs(Math.abs(month*2-15)-5)==2&&(day==31))||(month==2&&((day>28&&parseInt(year)%4!=0)
||(day>29&&parseInt(year)%4==0)))))
	{
		document.getElementById("Layer6").style.visibility="visible";
		return false;
	}
	else
	{
		document.getElementById("Layer6").style.visibility="hidden";
    		return true;
	}
}
//验证证件号码.
function validateidCard()
{
	var idType=document.frm1.idType.value;
	var idCard=document.frm1.idCard.value;
	if(idType==0)
	{
		if(idCard.length!=15 && idCard.length!=18)
		{
			document.getElementById("Layer7").style.visibility="visible";
			return false;
		}
		else
		{
			document.getElementById("Layer7").style.visibility="hidden";
    			return true;
		}
	}
	else
	{
		if(idCard.length<6)
		{
			document.getElementById("Layer8").style.visibility="visible";
			return false;
		}
		else
		{
			document.getElementById("Layer8").style.visibility="hidden";
    			return true;
		}
	}
}
//验证电子邮箱.
function validateEmail()
{
	var email=document.frm1.email.value;
	var length=email.length;
	var AtSym=email.indexOf("@");
	var period=email.indexOf(".");
	var space=email.indexOf(" ");
	var ch=email.charAt(length-1)
	var str=".@~!#$%^&*()_+|\=-}{';:?/.,<>";

	if(length>0)
	{
		if(AtSym<1 || period<=AtSym+1 || period==length-1 || space!=-1 || str.indexOf(ch)!=-1)
		{
			document.getElementById("Layer9").style.visibility="visible";
			return false;	
		}
	}
	else
	{
		document.getElementById("Layer9").style.visibility="hidden";
		return true;
	}
}

function DIVhidden1()
{
  document.getElementById("Layer1").style.visibility="hidden";
}
function DIVhidden2()
{
  document.getElementById("Layer2").style.visibility="hidden";
}
function DIVhidden3()
{
  document.getElementById("Layer3").style.visibility="hidden";
}
function DIVhidden4()
{
  document.getElementById("Layer4").style.visibility="hidden";
}
function DIVhidden5()
{
  document.getElementById("Layer5").style.visibility="hidden";
}
function DIVhidden6()
{
  document.getElementById("Layer6").style.visibility="hidden";
}
function DIVhidden78()
{
  document.getElementById("Layer7").style.visibility="hidden";
  document.getElementById("Layer8").style.visibility="hidden";
}
function DIVhidden9()
{
  document.getElementById("Layer9").style.visibility="hidden";
}
//验证表单是否能提交.
function validateform()
{
  if(validateusername()&&validatepassword()&&validateconfirmpassword()&&validatequestion()&&validateanswer()&&validateYear()&&validateDate()&&validateidCard()&&validateEmail())
  {
    return true;
  }
  else
  {
    return false;
  }
}
//验证用户名是否为指定字符组成.
function isSsnString (ssn)
{
	var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
	if(re.test(ssn))
		return true;
	else
		return false;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -