1、电子邮箱的验证
function IsEmail(object) {var strEmail = object.value;
if(strEmail==""){
return true;
}
if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/) != -1)
{return true;}
else
{
alert("Email格式不正确!");
object.value="";
object.focus();
return false;
}
}
2、网址的验证
function IsUrl(object)
{
var str = object.value;
if(str=="")
{
return true;
}
var urlreg=/^[A-Za-z0-9]+/.[A-Za-z0-9]+[//=/?%/-&_~`@[/]/':+!]*([^<>/"/"])*$/;
var url=str;
if (!urlreg.test(url))
{ alert("网址格式不正确!");
object.value="";
object.focus();
return false;
}
else{return true;
}
}
3、电话号码的验证,包括手机和固定电话
function checkPhone(object) {
var strPhone =object.value;
if(strPhone=="")
{
return true;
}
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,8}$/;
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;
if( strPhone.length > 9 ) {
if(strPhone.length==11){//手机号码验证
if(/^13/d{9}$/g.test(strPhone)||(/^15[8,9]/d{8}$/g.test(strPhone))){
return true;
}else{
alert("电话格式不正确!");
object.value="";
object.focus();
return false;
}
}
else if( phoneRegWithArea.test(strPhone) ){return true; }else{
alert("电话格式不正确!");
object.value="";
object.focus();
return false;}
}else
{
if(phoneRegNoArea.test(strPhone)){return true; }else{
alert("电话格式不正确!");
object.value="";
object.focus();
return false;
}
}
}
4、电话号码的验证,固定电话
function checkFixedPhone(object) {
var strPhone =object.value;
if(strPhone=="")
{
return true;
}
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,8}$/;
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;
if( strPhone.length > 9 ) {
if( phoneRegWithArea.test(strPhone) ){return true; }else{
alert("电话格式不正确!");
object.value="";
object.focus();
return false;}
}else
{
if(phoneRegNoArea.test(strPhone)){return true; }else{
alert("电话格式不正确!");
object.value="";
object.focus();
return false;
}
}
}
5、电话号码的验证,移动电话
function checkmobilePhone(object) {
var strPhone =object.value;
if(strPhone=="")
{
return true;
}
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,8}$/;
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/;
if( strPhone.length > 9 ) {
if(strPhone.length==11){//手机号码验证
if(/^13/d{9}$/g.test(strPhone)||(/^15[8,9]/d{8}$/g.test(strPhone))){
return true;
}else{
alert("手机号码格式不正确!");
object.value="";
object.focus();
return false;
}
}
else if( phoneRegWithArea.test(strPhone) ){return true; }else{
alert("手机号码格式不正确!");
object.value="";
object.focus();
return false;}
}
else{
alert("手机号码格式不正确!");
object.value="";
object.focus();
return false;
}
}
6、验证邮政编码
function checkTextDataForPOST(object)
{
var strValue = object.value;
if(strValue=="")
{
return true;
}
var regTextPost = /^(/d){6}$/;
if(regTextPost.test(strValue))
{return;
}
else{alert("邮政编码有错!");
object.focus();
object.value="";
return;
}
}
7、验证是否数字
function test_Number(p){
var strValue = p.value;
if(strValue=="")
{
return true;
}
if (!isNaN(strValue)){
return true;}
else{
alert("请输入有效数字...");
p.focus();
p.value="";
return false;
}
}
8、检查年龄
function isAge(p){
var strValue = p.value;
if(strValue=="")
{
return true;
}
// var mydate=new Date;
// var now=mydate.getFullYear();
if (parseInt(strValue) > 60 || parseInt(strValue) < 18){
alert("请输入有效年龄...");
p.focus();
p.value="";
return false;
}
return true;
}
9、比较时间 DateOne比DateTwo晚返回true
function compareDate(DateOne,DateTwo)
{
var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ("-"));
var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ("-")+1);
var OneYear = DateOne.substring(0,DateOne.indexOf ("-"));
var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ("-"));
var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ("-")+1);
var TwoYear = DateTwo.substring(0,DateTwo.indexOf ("-"));
if (Date.parse(OneMonth+"/"+OneDay+"/"+OneYear) >
Date.parse(TwoMonth+"/"+TwoDay+"/"+TwoYear))
{
return true;
}
else
{
return false;
}
}