JavaScript,javascript教程

來源:互聯網
上載者:User

JavaScript,javascript教程
Regex

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Regex</title><style type="text/css">    .ok{            background: url(images/ok.gif) no-repeat;           }</style><script type="text/javascript">     //出生日期在1900-2014之間,格式為yyyy-mm-dd     function checkBirthday(){        var regBirthday=/^(19\d{2}|200\d|201[0-4])-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/;        var birthday=$("birthday").value;        if(!regBirthday.test(birthday)){            $("birthdayInfo").style.color="#f00";            $("birthdayInfo").innerHTML=" * 格式不對,出生日期在1900-2014之間,格式為yyyy-mm-dd";            $("birthdayInfo").className="";        }else{            $("birthdayInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("birthdayInfo").className="ok";        }     }    //手機號碼,11位,以1開始    function checkMobile(){        var regMobile=/^1\d{10}$/;        var mobile=$("mobile").value;        if(!regMobile.test(mobile)){            $("mobileInfo").style.color="#f00";            $("mobileInfo").innerHTML=" * 手機號碼,11位,以1開始";            $("mobileInfo").className="";        }else{            $("mobileInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("mobileInfo").className="ok";        }    }    //郵遞區號,6位,首位不為0    function checkCode(){        var regCode=/^[1-9]\d{5}$/;        var code=$("code").value;        if(!regCode.test(code)){            $("codeInfo").style.color="#f00";            $("codeInfo").innerHTML=" * 郵遞區號,6位,首位不為0";            $("codeInfo").className="";        }else{            $("codeInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("codeInfo").className="ok";        }               }    //使用者名稱,由字母、數字、底線、點號、橫線、中文字元組成,    //只能以數字、字母開頭或結尾,且長度為6-14    function checkUsername(){        var regUsername=/^([\d|[a-z])([\u4e00-\u9fa5\w.-]) {4,12}([\d|[a-z])$/i;        var username=$("username").value;        if(!regUsername.test(username)){            $("usernameInfo").style.color="#f00";            $("usernameInfo").innerHTML=" * 使用者名稱,由字母、數字、底線、點號、橫線、中文字元組成";            $("cusernameInfo").className="";        }else{            $("usernameInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("usernameInfo").className="ok";        }               }    /^\d|[a-z]{4,10}$/;    //密碼,只能由字母和數字組成,長度為4-10位    function checkPwd(){        var regPwd=/^[a-zA-Z0-9]{4,10}$/;        var pwd=$("pwd").value;        if(!regPwd.test(pwd)){            $("pwdInfo").style.color="#f00";            $("pwdInfo").innerHTML=" * 密碼,只能由字母和數字組成,長度為4-10位";            $("pwdInfo").className="";        }else{            $("pwdInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("pwdInfo").className="ok";        }                   }    //年齡,1-120    function checkAge(){        var regAge=/^(1[0-1]|[1-9])?\d$/;        var age=$("age").value;        if(!regAge.test(age)){            $("ageInfo").style.color="#f00";            $("ageInfo").innerHTML=" * 年齡,1-120";            $("ageInfo").className="";        }else{            $("ageInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("ageInfo").className="ok";        }               }    //判斷身份證格式(要求支援新舊證)    function checkIDCard(){        var regIDCard=/^[1-9]\d{14}(\d{2}[0-9X])?$/;        var IDCard=$("IDCard").value;        if(!regIDCard.test(IDCard)){            $("IDCardInfo").style.color="#f00";                    $("IDCardInfo").innerHTML=" * 不是一代或二代身份證!";            $("IDCardInfo").className="";        }else{            $("IDCardInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("IDCardInfo").className="ok";        }               }    //    function checkPhone(){    }    //判斷Email地址    function checkEmail(){        var regEmail=/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;        var email=$("email").value;        if(!regEmail.test(email)){            $("emailInfo").style.color="#f00";                     $("emailInfo").innerHTML=" * Email地址不正確!";            $("emailInfo").className="";        }else{            $("emailInfo").innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";            $("emailInfo").className="ok";        }               }    function $(id){        return document.getElementById(id)    }       </script></head><body>    出生日期:<input type="text" id="birthday" onblur="checkBirthday()"/><span id="birthdayInfo"></span><br/>    手機號碼:<input type="text" id="mobile" onblur="checkMobile()"/><span id="mobileInfo"></span><br/>    郵遞區號:<input type="text" id="code" onblur="checkCode()"/><span id="codeInfo"></span><br/>    使用者名稱:<input type="text" id="username" onblur="checkUsername()"/><span id="usernameInfo"></span><br/>    密碼:<input type="password" id="pwd" onblur="checkPwd()"/><span id="pwdInfo"></span><br/>    年齡:<input type="text" id="age" onblur="checkAge()"/><span id="ageInfo"></span><br/>    身份證:<input type="text" id="IDCard" onblur="checkIDCard()"/><span id="IDCardInfo"></span><br/>    固定電話:<input type="text" id="phone" onblur="checkPhone()"/><span id="phoneInfo"></span><br/>    電子郵箱:<input type="text" id="email" onblur="checkEmail()"/><span id="emailInfo"></span><br/></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.