//file: check.js
//校验是否全由数字组成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校验用户姓名:只能输入1-30个以字母开头的字串
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}
//校验密码:只能输入6-20个字母、数字、下划线
function isPasswd(s)
{
var patrn=/^(\w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(s)
{
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验手机号码:必须以数字开头,除数字外,可含有“-”
function isMobil(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验邮政编码
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
//邮件检测
function isEmail(theStr) {
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.', atIndex);
var flag = true;
theSub = theStr.substring(0, dotIndex+1)
if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
{ return(false); }
else { return(true); }
}
//判断是否为空
function isEmpty (str) {
if ((str==null)||(str.length==0)) return true;
else return(false);
}
//判断日期
function isDate(theStr) {
var the1st = theStr.indexOf('-');
var the2nd = theStr.lastIndexOf('-');
if (the1st == the2nd) {
return false;
} else {
var y = theStr.substring(0,the1st);
var m = theStr.substring(the1st+1,the2nd);
var d = theStr.substring(the2nd+1,theStr.length);
var maxDays = 31;
if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return false;
} else if (y.length < 4) {
return false ;
} else if (!isBetween (m, 1, 12)) {
return false;
} else if (m==4 || m==6 || m==9 || m==11) {
maxDays = 30;
} else if (m==2) {
if (y % 4 > 0) {
maxDays = 28;
}else if (y % 100 == 0 && y % 400 > 0) {
maxDays = 28;
}else{
maxDays = 29;
}
}
if (isBetween(d, 1, maxDays) == false) {
return false;
}
else { return true; }
}
}
//判断范围
function isBetween (val, lo, hi) {
if ((val < lo) || (val > hi)) { return(false); }
else { return(true); }
}
//判断是否为Int型
function isInt (theStr) {
var flag = true;
if (isEmpty(theStr)) { flag=false; }
else
{ for (var i=0; i<theStr.length; i++) {
if (isDigit(theStr.substring(i,i+1)) == false) {
flag = false; break;
}
}
}
return(flag);
}
//表单元素通用验证方法
//NU 检测是否为空
//NM 检测是否为数字
//EM 检测是否为MAIL
//LE=12 检测长度
//YY 年份
//MM 月份
//DD 日
function validateForm(add){
var alertMessage = "";
var elArr = eval(document.forms[0].name).elements;
var strYY = "";
var strMM = "";
var strDD = "";
for(var i = 0; i < elArr.length; i++){
if(elArr[i].id){
var aryList = (elArr[i].id).split(',');
for(var j=0; j<aryList.length; j++){
if(aryList[j].substring(0,2) == "NU"){
if(elArr[i].value==""){
alertMessage = alertMessage + elArr[i].title + "不能为空!\n";
break;
}
}
if(aryList[j].substring(0,2) == "NM"){
if(elArr[i].value !=""){
if(isDigit(elArr[i].value)==false){
alertMessage = alertMessage + elArr[i].title + "不为数字!\n";
break;
}
}
}
if(aryList[j].substring(0,2) == "EM"){
if(elArr[i].value !=""){
if(isEmail(elArr[i].value)==false){
alertMessage = alertMessage + elArr[i].title + "格式有误!\n";
break;
}
}
}
if(aryList[j].substring(0,2) == "LE"){
if(elArr[i].value !=""){
var eleLength = elArr[i].value.length;
var ordLength = aryList[j].substring(3,100);
if( eleLength > ordLength){
alertMessage = alertMessage + elArr[i].title + "长度要求为"+ ordLength +"位!\n";
break;
}
}
}
if(aryList[j].substring(0,2) == "YY"){
strYY = elArr[i].value;
}
if(aryList[j].substring(0,2) == "MM"){
strMM = elArr[i].value;
}
if(aryList[j].substring(0,2) == "DD"){
strDD = elArr[i].value;
}
if(strYY != "" && strMM != "" && strDD != ""){
if(strYY != "" && strMM != "" && strDD != ""){
var strDate = strYY + "-" + strMM +"-"+ strDD;
if(isDate(strDate) == false){
alertMessage = alertMessage + "日期格式不正确\n";
strYY = "";
strMM = "";
strDD = "";
}
}
}
}
}
}
if(add == "true"){
alertMessage = addMessage(alertMessage);
}
if(alertMessage != ""){
alert(alertMessage);
return false;
}else{
return true;
}
}
function checkboxChecked(checkboxName)
{
var count=0;
var formname = document.forms[0].name;
if(eval("document." + formname + "." + checkboxName).length!=null)
{
for (var i = 0; i < eval("document." + formname + "." + checkboxName).length; i++)
{
if (eval("document." + formname + "." + checkboxName)[i].checked == true)
{
count=count+1;
}
}
}
else
{
if (eval("document." + formname + "." + checkboxName).checked == true)
{
count=count+1;
}
}
return count;
}
//file: index.html
<html>
<script language="JavaScript" src="check.js"></script>
<script>
function addMessage(message){
message = message + "另加的错误信息\n"
return message;
}
</script>
<body>
<form name="ordForm" id="ordForm" method="post">
<input type="test" name="name" id="NU,NM" title="姓名">
<input type="test" name="password" id="NU,LE=12" title="密码">
<input type="test" name="mail" id="NU,EM" title="邮件">
<input type="test" name="yyBIRTHDAY" id="YY" title="姓名">
<input type="test" name="mmBIRTHDAY" id="MM" title="密码">
<input type="test" name="ddBIRTHDAY" id="DD" title="邮件">
<input type="button" value="submit" onClick="validateForm(ordForm)">
</form>
</body>
</html>
//表单元素通用验证方法
//NU 检测是否为空
//NM 检测是否为数字
//EM 检测是否为MAIL
//LE=12 检测长度
//YY 年份
//MM 月份
//DD 日