js 技巧 (八)JS代码判断集锦(之二)
JS代码判断集锦(之二)
<INPUT TYPE="button" value="登录" tabindex="4">
<INPUT TYPE="button" value="注册" tabindex="4">
//////////////////////////////////////////////
function trim(inputstr) //去处空格,在获取客户端录入的表单信息前,
都先trim一下再提交 例如:“form1.org_name.value = trim(form1.org_name.value);”
{
if(!inputstr.length>0) return "";
var starts=0;
var ends=inputstr.length;
while(starts<ends)
{
if(inputstr.substring(starts,starts+1)==" ")
starts++;
else
break;
}
while(starts<ends)
{
if(inputstr.substring(ends-1,ends)==" ")
ends--;
else
break;
}
return inputstr.substring(starts,ends);
}
function isvalidcode(inputs) //校验邮政编码
{
var i,temp;
var isvalidcode = true;
inputstr=trim(inputs);
if(inputstr.length==null||inputstr.length==0||inputstr.length!=6) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!(temp>="0" && temp<="9"))
{
isvalidcode=false;
break;
}
}
return isvalidcode;
}
function isvalidtel(inputs) //校验电话号码
{
var i,temp;
var isvalidtel = true;
inputstr=trim(inputs);
if(inputstr.length==null||inputstr.length==0) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!(temp>="0" && temp<="9" || temp=="-"))
{
isvalidtel=false;
break;
}
}
return isvalidtel;
}
function isrealnum(inputs) //校验实数(含小数点)
{
var i,temp;
var isrealnum = true;
inputstr=trim(inputs);
if(inputstr.length==null||inputstr.length==0) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!(temp>="0" && temp<="9" || temp=="."))
{
isrealnum=false;
break;
}
}
return isrealnum;
}
function isnumeric(inputs) //校验是否整数
{
var i,temp;
var isnumeric = true;
inputstr=trim(inputs);
if(inputstr.length==null||inputstr.length==0) return false;
//isvalid = true;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!(temp>="0" && temp<="9"))
{
isnumeric=false;
break;
}
}
return isnumeric;
}
function isvalidide(inputs) //校验身份证
{
var i,temp;
var isvalidide = true;
inputstr=trim(inputs);
if(inputstr.length == null || inputstr.length == 0 || (inputstr.length != 15 && inputstr.length != 18)) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!((temp>="0" && temp<="9") || temp == "x" || temp == "X"))
{
isvalidide=false;
break;
}
}
return isvalidide;
}
function isvalidname(inputs) //校验账号2~15位(即只能是英文字母和数字组成的串)
{
var i,temp;
var isvalidname = true;
inputstr=trim(inputs);
if(inputstr.length==null || inputstr.length==0 || inputstr.length > 15 || inputstr.length < 2) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!((temp >= "0" && temp <= "9") || (temp >= "a" && temp <= "z") || (temp >= "A" && temp <= "Z")))
{
isvalidname=false;
break;
}
}
return isvalidname;
}
function isvalidphoto(inputs) //校验照片扩展名
{
var i,temp;
var isvalidphoto = true;
inputstr=trim(inputs);
if(inputstr.length==null || inputstr.length==0) return true; //没有相片也允许的
temp=inputstr.substring(inputstr.lastIndexOf(".")+1);
temp = temp.toLowerCase();
if(!(temp == "bmp" || temp == "jpg" || temp == "gif"))
{
isvalidphoto=false;
}
return isvalidphoto;
}
function isvalidzgh(inputs) //校验职工号8位(即只能是英文字母和数字组成的串)
{
var i,temp;
var isvalidzgh = true;
inputstr=trim(inputs);
if(inputstr.length==null || inputstr.length==0 || inputstr.length != return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!((temp >= "0" && temp <= "9") || (temp >= "a" && temp <= "z") || (temp >= "A" && temp <= "Z")))
{
isvalidzgh=false;
break;
}
}
return isvalidzgh;
}
function isvalidxh(inputs) //校验学号7位(即只能是英文字母和数字组成的串)
{
var i,temp;
var isvalidxh = true;
inputstr=trim(inputs);
if(inputstr.length==null || inputstr.length==0 || inputstr.length != 7) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!((temp >= "0" && temp <= "9") || (temp >= "a" && temp <= "z") || (temp >= "A" && temp <= "Z")))
{
isvalidxh=false;
break;
}
}
return isvalidxh;
}
function isvalidkcid(inputs) //校验课程编号5位(即只能是数字组成的串)
{
var i,temp;
var isvalidkcid = true;
inputstr=trim(inputs);
if(inputstr.length==null||inputstr.length==0||inputstr.length!=5) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!(temp>="0" && temp<="9"))
{
isvalidkcid=false;
break;
}
}
return isvalidkcid;
}
function isvalidnamee(inputs) //校验账号4~20位(即只能是英文字母和数字组成的串)
{
var i,temp;
var isvalidname = true;
inputstr=trim(inputs);
if(inputstr.length==null || inputstr.length==0 || inputstr.length > 20 || inputstr.length < 4) return false;
for(i=0;i<inputstr.length;i++)
{
temp=inputstr.substring(i,i+1);
if(!((temp >= "0" && temp <= "9") || (temp >= "a" && temp <= "z") || (temp >= "A" && temp <= "Z")))
{
isvalidname=false;
break;
}
}
return isvalidname;
}
==========================
<Script language="JavaScript">
function login()
{
form1.UNAME.value = trim(form1.UNAME.value);
form1.PWD.value = trim(form1.PWD.value);
form1.PWD2.value = trim(form1.PWD2.value);
form1.EMAIL.value = trim(form1.EMAIL.value);
form1.NC.value = trim(form1.NC.value);
if(!isvalidname(form1.UNAME.value))
{
alert("请您注意:用户名必须为『2~15位英文或数字』!");
form1.UNAME.focus();
return false;
}
else if(!isvalidname(form1.PWD.value))
{
alert("请您注意:密码必须为『2~15位英文或数字』!");
form1.PWD.focus();
return false;
}
else if(!isvalidname(form1.PWD2.value))
{
alert("请您注意:密码确认也必须为『2~15位英文或数字』!");
form1.PWD2.focus();
return false;
}
else if(form1.PWD.value != form1.PWD2.value)
{
alert("两次密码不一致!");
form1.PWD.focus();
return false;
}
else if(form1.EMAIL.value == "" || form1.EMAIL.value.length > 100)
{
alert("请输入邮件!");
form1.EMAIL.focus();
return false;
}
else if(form1.NC.value == "" || form1.NC.value.length > 15)
{
alert("请输入昵称!");
form1.NC.focus();
return false;
}
else if(window.confirm("您确信提交!"))
return true;
else
return false;
}
function regedit()
{
var litop = (screen.height - 162)/2;
var lileft = (screen.width - 360)/2;
window.open("/regedit.aspx", "reg", "titlebar=no,fullscreen=no,dependent=yes,height=162,width=360,
top=" + litop + ",left=" + lileft + ",
toolbar=no,menubar=no,scrollbars=no,resizable=no,
channelmode=no,status=no,location=no,directories=no");
}
</Script>
出处至:http://xiaobingandxiaoer.iteye.com/blog/1917696