JS表单判断函数代码大全.docx
《JS表单判断函数代码大全.docx》由会员分享,可在线阅读,更多相关《JS表单判断函数代码大全.docx(34页珍藏版)》请在冰点文库上搜索。
JS表单判断函数代码大全
JS表单判断函数代码大全.txt我爸说过的最让我感动的一句话:
“孩子,好好学习吧,爸以前玩麻将都玩儿10块的,现在为了供你念书,改玩儿1块的了。
”/*程序功能:
输入验证*/
/*函数名称:
*/
/*functionCheckData(valname,val,valimode,limitlen)*/
/*功能说明:
验证字符串数据*/
/*functionCheckUserName(val,min,max)*/
/*功能说明:
验证用户名*/
/*functionCheckPassWord(val,min,max)*/
/*功能说明:
验证密码*/
/*functionIsSame(val1,val2)*/
/*验证密码匹配*/
/*functionCheckEmail(val,mode)*/
/*功能说明:
验证Email*/
/*functionCheckValHeight(val,min,max,mode)*/
/*功能说明:
验证用户身高*/
/*functionCheckValWeight(val,min,max,mode)*/
/*功能说明:
验证用户体重*/
/*functionCheckNumber(val,min,max,mode)*/
/*功能说明:
验证数值*/
/*functionCheckPositive(val,min,max,mode)*/
/*功能说明:
验证正数值*/
/*functionCheckNegative(val,min,max,mode)*/
/*功能说明:
验证负数值*/
/*functionCheckFloat(val,min,max,mode)*/
/*功能说明:
验证浮点数值*/
/*functionCheckPath(val,mode)*/
/*功能说明:
验证文件路径*/
/*functionCheckURL(val,mode)*/
/*功能说明:
验证URL*/
/*functionCheckChinaIDCard_J(val,mode)*/
/*功能说明:
验证身份证*/
/*functionisInteger(sNum)*/
/*功能说明:
验证整数*/
/*functionCheckZip(val,slen,mode)*/
/*功能说明:
验证Zip*/
/*输入参数:
mode真&假(是否允许为空)*/
/*val表单(被判断项),va2表单(被判断项)*/
/*max(最大值),min(最小值)......*/
/*编写时间:
2003.09.08.2214*/
/*$#################################################$*/
//验证字符串数据
functionCheckData(valname,val,valimode,limitlen){
if(val.value.indexOf("<",0)>0||val.value.indexOf(">",0)>0){
alert(valname+"不能包含<或>字符!
。
");
val.focus();
val.select();
returnfalse;
}
if(valimode==1){
if(isblank(val.value)){
alert(valname+"不能为空!
");
val.focus();
val.select();
returnfalse;
}
tmp=newString(val.value)
if(istoolong(tmp,limitlen)){
alert(valname+"不能超过"+limitlen+"个字符!
");
val.focus();
val.select();
returnfalse;
}
}
else{
tmp=newString(val.value)
if(istoolong(tmp,limitlen)){
alert(valname+"不能超过"+limitlen+"个字符!
!
");
val.focus();
val.select();
returnfalse;
}
}
returntrue;
}
functionisblank(str){
vari;
varlen=str.length;
for(i=0;iif(str.charAt(i)!
="")
returnfalse;
}
returntrue;
}
functionistoolong(str,i){
varlen=str.length;
if(i==0)
returnfalse;
else{
if(len>i)
returntrue;
else
returnfalse;
}
}
//验证用户名
functionCheckUserName(val,min,max){
varlen=val.value.length;
if(lenmax){
alert("用户名长度不正确,应为"+min+"-"+max+"个英文字母、数字。
");
val.focus();
val.select();
returnfalse;
}
for(i=0;ivarch=val.value.charAt(i);
if((ch<"0"||ch>"9")&&(ch<"a"||ch>"z")){
alert("用户名必须由小写字母或数字组成。
");
val.focus();
val.select();
returnfalse;
}
}
varfirst=val.value.charAt(0)
if(first<"a"||ch>"z"){
alert("用户名必须由小写字母开头。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证密码
functionCheckPassWord(val,min,max){
varlen=val.value.length;
if(lenmax){
alert("密码长度不正确,应为"+min+"-"+max+"个英文字母、数字。
");
val.focus();
val.select();
returnfalse;
}
for(i=0;ivarch=val.value.charAt(i);
if((ch<"0"||ch>"9")&&(ch<"a"||ch>"z")){
alert("密码必须由小写字母或数字组成。
");
val.focus();
val.select();
returnfalse;
}
}
returntrue;
}
//验证密码匹配
functionIsSame(val1,val2){
if(val1.value!
=val2.value){
alert("两次输入的密码不相同。
");
val2.focus();
val2.select();
returnfalse;
}
else
returntrue;
}
//验证Email
functionCheckEmail(val,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varetext
varelen
vari
varaa
etext=val.value
elen=etext.length
if(elen<5){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
i=etext.indexOf("@",0)
if(i==0||i==-1||i==elen-1){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
else{
if(etext.indexOf("@",i+1)!
=-1){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
}
if(etext.indexOf("..",i+1)!
=-1){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
i=etext.indexOf(".",0)
if(i==0||i==-1||etext.charAt(elen-1)=='.'){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
if(etext.charAt(0)=='-'||etext.charAt(elen-1)=='-'){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
if(etext.charAt(0)=='_'||etext.charAt(elen-1)=='_'){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
for(i=0;i<=elen-1;i++){
aa=etext.charAt(i)
if(!
((aa=='.')||(aa=='@')||(aa=='-')||(aa=='_')||(aa>='0'&&aa<='9')||(aa>='a'&&aa<='z')||(aa>='A'&&aa<='Z'))){
alert("请输入正确的Email。
");
val.focus();
val.select();
returnfalse;
}
}
returntrue;
}
//验证用户身高
functionCheckValHeight(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varlen=val.value.length;
if(lenmax){
alert("请输入正确的身高数据。
");
val.focus();
val.select();
returnfalse;
}
varval2=val.value;
for(i=0;ivarch=val2.charAt(i);
if((ch<"0"||ch>"9")){
alert("身高数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
}
if(isNaN(val2)){
alert("身高数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
elseif(parseInt(val2)<=0){
alert("身高数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('0')==0){
alert("身高数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证用户体重
functionCheckValWeight(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varlen=val.value.length;
if(lenmax){
alert("请输入正确的体重数据。
");
val.focus();
val.select();
returnfalse;
}
varval2=val.value;
for(i=0;ivarch=val2.charAt(i);
if((ch<"0"||ch>"9")){
alert("体重数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
}
if(isNaN(val2)){
alert("体重数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
elseif(parseInt(val2)<=0){
alert("体重数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('0')==0){
alert("体重数据必须由有效数字组成。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证数值
functionCheckNumber(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varlen=val.value.length;
if(lenmax){
alert("请输入正确的数值。
");
val.focus();
val.select();
returnfalse;
}
varval2=val.value;
for(i=1;ivarch=val2.charAt(i);
if((ch<"0"||ch>"9")){
alert("请输入正确的数值。
");
val.focus();
val.select();
returnfalse;
}
}
if(isNaN(val2)){
alert("请输入正确的数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('0')==0&&len>1){
alert("请输入正确的有效数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('-')==0&&val2.indexOf('0')==1){
alert("请输入正确的有效数值。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证正数值
functionCheckPositive(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
if(!
CheckNumber(val,min,max,mode)){
val.focus();
val.select();
returnfalse;
}
if(parseInt(val.value)<=0){
alert("数据必须为有效的正整数。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证负数值
functionCheckNegative(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
if(!
CheckNumber(val,min,max,mode)){
val.focus();
val.select();
returnfalse;
}
if(parseInt(val.value)>=0){
alert("数据必须为有效的负整数。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证浮点数值
functionCheckFloat(val,min,max,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varlen=val.value.length;
if(lenmax){
alert("请输入正确的浮点数值。
");
val.focus();
val.select();
returnfalse;
}
varval2=val.value;
for(i=1;ivarch=val2.charAt(i);
if((ch<"0"||ch>"9")){
if(ch!
="."){
alert("请输入正确的浮点数值。
");
val.focus();
val.select();
returnfalse;
}
}
}
if(isNaN(val2)){
alert("请输入正确的浮点数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('0')==0&&val2.indexOf('.')!
=1){
alert("请输入正确的有效浮点数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('-')==0&&val2.indexOf('0')==1&&val2.indexOf('.')!
=2){
alert("请输入正确的有效浮点数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('-')==0&&val2.indexOf('.')==1){
alert("请输入正确的有效浮点数值。
");
val.focus();
val.select();
returnfalse;
}
elseif(val2.indexOf('.')==0){
alert("请输入正确的有效浮点数值。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证文件路径
functionCheckPath(val,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varval2=val.value;
if(val2.indexOf(':
\\')>0){
varisNot="!
@#$^*()'`~|]}[{;.>,
%&+=";
if(val2.indexOf('\"')>0){
alert("请输入正确的文件路径。
");
val.focus();
val.select();
returnfalse;
}
else{
for(vari=0;ifor(varx=1;xif(val2.charAt(i)==isNot.charAt(x)){
alert("请输入正确的文件路径。
");
val.focus();
val.select();
returnfalse;
}
}
}
}
}
else{
alert("请输入正确的文件路径。
");
val.focus();
val.select();
returnfalse;
}
returntrue;
}
//验证URL
functionCheckURL(val,mode){
if((mode==0)&&(val.value=="")){
returntrue;
}
varval2=val.value;
if(val2.indexOf(':
//')>0){
varisNot="!
@$^*()'`~|]}[{;.>,<";
if(val2.indexOf('\"')>0){
alert("请输入正确的URL。
");
val.focus();
val.select();
returnfalse;
}
else{
for(vari=0;ifor(varx=1;xif(val2.charAt(i)==isNot.charAt(x)){
alert("请输入正确的URL。
");
val.focus();
val.select();
returnfalse;
}
}
}
}
}
else