//把下面的文字保存为js文件
//******************************************************
//验证是否为空,如果为空返回 false,否则返回 true
function CheckIsNull(Inform,Inputname){
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
if(Temp==""){
alert("提醒您:不能为空");
eval(Form+Inputname+".className='RedInput';");
eval(Form+Inputname+".focus();");
return false;
}else{
eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
return true;
}
}
//验证是否为数字
function CheckIsNum(Inform,Inputname){
if (!CheckIsNull(Inform,Inputname))return false;
else{
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
if(isNaN(Temp)){
alert("提醒您:不为数字");
eval(Form+Inputname+".className='RedInput';");
eval(Form+Inputname+".focus();");
return false;
}
else{
eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
return true;
}
}
}
//验证是否为E-MAIL
function CheckIsEmail(Inform,Inputname){
if (!CheckIsNull(Inform,Inputname))return false;
else{
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
if(Temp.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1)
{ alert("提醒您:不为EMAIL");
eval(Form+Inputname+".className='RedInput';");
eval(Form+Inputname+".focus();");
return false;
}
else{
eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
return true;
}
}
}
//验证是否为HTTP地址
function CheckIsHttp(Inform,Inputname){
if (!CheckIsNull(Inform,Inputname))return false;
else{
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
if(Temp.search(/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/)==-1)
{ alert("提醒您:不为HTTP");
eval(Form+Inputname+".className='RedInput';");
eval(Form+Inputname+".focus();");
return false;
}
else{
eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
return true;
}
}
}
//验证是否为手机号码
function CheckIsMobile(Inform,Inputname){
if (!CheckIsNull(Inform,Inputname))return false;
else{
var Form=Inform+"."
eval("Temp="+Form+Inputname+".value;");
if(Temp.search(/^1[3|5]\d{9}$/)==-1)
{ alert("提醒您:不为手机号码");
eval(Form+Inputname+".className='RedInput';");
eval(Form+Inputname+".focus();");
return false;
}
else{
eval(Form+Inputname+".className="+Form+Inputname+".className.replace('RedInput','');");
return true;
}
}
}
//*****************************
使用方法
index.html
<script language="javascript" src="***.js"></script>
//总验证函数
//调用参数说明:(formname,inputname) 其中formname为表单名,inputname为输入文本框名
function CheckForm(){
if(!CheckIsHttp("myform","mytxt")) return false;
}
//-->
</script>
<style type="text/css">
.RedInput {background-color:#FF0000;}
</style>
<form id="myform" name="myform" method="post" action=""onSubmit="return CheckForm()">
<input type="text" name="mytxt" />
<input type="submit" name="Submit" value="提交" />
</form>