常用javascript 方法

 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;
      }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值