formcheck.js
---------------------------------------------------------
/*------------------------------------------------------------------------------* File: formcheck.js
* Copyright Mr. Fengjun Zhao. All Rights Reserved.
* Author: 赵凤君@09/20/2002
* Modify:
* Version: 1.0
* Description:FormCheck组件的函数库文件(部分),共计110个方法
*------------------------------------------------------------------------------*/
/*焦点放置在第一个表单域*/
function focusInFirst(){
document.forms[0].elements[0].focus();
}
/*判断用户输入是否为空*/
function isEmpty(ui){
return (ui==null||ui=="");
}
/*取得用户输入的字符串的长度*/
function getLength(ui){
var i,sum=0;
for(i=0;i<ui.length;i++){
if ((ui.charCodeAt(i)>=0) && (ui.charCodeAt(i)<=255))
sum++;
else
sum+=2;
}
return sum;
}
/*是否为数字、字母或下划线*/
function isNLU(ui){
var valid=/^\w*$/;
return (valid.test(ui));
}
/*判断是否为身份证号码*/
function isIdno(ui){
var valid=/(^\d{16}$)|(^\d{18}$)/;
return (isEmpty(ui)||valid.test(ui));
}
/*判断是否为邮政编码*/
function isPostCode(ui){
var valid=/^\d{6}$/;
return (isEmpty(ui)||valid.test(ui));
}
/*判断是否为固定电话*/
function isChinaTel(ui){
var valid=/^0\d{2,3}\-\d{7,8}$/;
return (isEmpty(ui)||valid.test(ui));
}
/*判断是否为移动电话*/
function isChinaMob(ui){
var valid=/^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/;
;
return (isEmpty(ui)||valid.test(ui));
}
/*判断是否为电话,只能为固定电话或移动电话*/
function isTel(ui){
var valid=/(^0\d{2,3}\-\d{7,8}$)|(^0?13[0,1,3,5,6,8,9]\d{8}$)/;
return (isEmpty(ui)||valid.test(ui));
}
/*判断是否为邮件*/
function isMail(ui){
if(isEmpty(ui)){return true;}
var notValid=/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(^\-)|(\-\.)|(\.\-)/;
var valid=/^.+\@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}$/;
return (!notValid.test(ui)&&valid.test(ui));
}
/*用户输入字符串长度是否在两值之间*/
function isLenBetween(ui,minl,maxl){
return (ui>=minl&&ui<=maxl);
}
--------------------------------------------------------
a.html
-------------------------------------------------------
<HTML><HEAD><TITLE>FormCheck</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<STYLE>TD {FONT-SIZE: 12px; COLOR: #333333}</STYLE>
<SCRIPT language=JavaScript type=text/JavaScript src="formcheck.js"></SCRIPT>
<SCRIPT language=JavaScript>
<!--
function formCheck(){
var len=getLength(document.testForm.leng.value);
if( document.testForm.id.value==""){
alert("身份证号码不能为空");
document.testForm.id.focus();
return false;
focusInFirst(1);
}else if(!isIdno(document.testForm.id.value)){
alert("身份证号码输入有误!要求只能输入数字,长度为16或18位。");
document.testForm.id.focus();
return false;
focusInFirst(1);
}else if(document.testForm.tel.value==""){
alert("固定电话号码不能为空");
document.testForm.tel.focus();
return false;
}else if(!isChinaTel(document.testForm.tel.value)){
alert("固定电话号码输入有误!合法的格式为:区号-电话号码。");
document.testForm.tel.focus();
return false;
}
else if(document.testForm.mobi.value==""){
alert("移动电话号码不能为空!");
document.testForm.mobi.focus();
return false;
}
else if(!isChinaMob(document.testForm.mobi.value)){
alert("移动电话号码输入有误!");
document.testForm.mobi.focus();
return false;
}
else if(document.testForm.mail.value==""){
alert("邮箱不能为空!");
document.testForm.mail.focus();
return false;
}
else if(!isMail(document.testForm.mail.value)){
alert("邮箱输入有误!");
document.testForm.mail.focus();
return false;
}
else if(document.testForm.post.value==""){
alert("邮政编码不能为空");
document.testForm.post.focus();
return false;
}
else if(!isPostCode(document.testForm.post.value)){
alert("邮政编码输入有误!要求只能输入数字,长度为6位。");
document.testForm.post.focus();
return false;
}
else if(document.testForm.nlu.value==""){
alert("该域不能为空!");
document.testForm.nlu.focus();
return false;
}
else if(!isNLU(document.testForm.nlu.value)){
alert("该域只能输入数字、字母或下划线!");
document.testForm.nlu.focus();
return false;
}
else if(document.testForm.leng.value==""){
alert("该域2不能为空!");
document.testForm.leng.focus();
return false;}
else if(len<4||len>6){
alert("该域要求输入长度范围是4至6个字符!");
document.testForm.leng.focus();
return false;}
alert('检验通过,马上提交!');
return true;
}
//-->
</SCRIPT>
<BODY text=#000000 bgColor=#ffffff οnlοad=focusInFirst()>
<CENTER>
<B>
<FONT face="Geneva, Arial, Helvetica, san-serif" color=#ff6600 size=4>The sample of FormCheck</FONT>
</B>
<FONT face="Arial, Helvetica, sans-serif" size=2>(By Roczhao) </FONT>
<HR width="60%" color=#000000 noShade SIZE=1>
<FORM name=testForm οnsubmit="return formCheck();" method=post>
<TABLE cellSpacing=0 cellPadding=0 width=500 border=1>
<TBODY>
<TR>
<TD width=182 height=25>身份证:</TD>
<TD width=312><INPUT name=id> </TD></TR>
<TR>
<TD width=182 height=25>固定电话:</TD>
<TD width=312><INPUT name=tel> </TD></TR>
<TR>
<TD width=182 height=25>手机号码:</TD>
<TD width=312><INPUT name=mobi> </TD></TR>
<TR>
<TD width=182 height=25>邮箱:</TD>
<TD width=312><INPUT name=mail> </TD></TR>
<TR>
<TD width=182 height=25>邮编:</TD>
<TD width=312><INPUT name=post> </TD></TR>
<TR>
<TD width=182 height=25>只能输入数字、字母、下划线:</TD>
<TD width=312><INPUT name=nlu> </TD></TR>
<TR>
<TD width=182 height=25>输入长度在4-6之间:</TD>
<TD width=312><INPUT name=leng> </TD></TR>
<TR align=center>
<TD colSpan=2 height=25><INPUT type=submit value=" 点击测试 " name=Submit>
</TD></TR></TBODY></TABLE></FORM></CENTER></BODY></HTML>