javascriptでよく使われる正規表現

4648 ワード

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h3>         </h3>
<input type="text" name="number" value="      " id="banNumber" />
<input type="text" id="chineseStr" value="  (  )">
<input type="text" value="    " id="email">
<input type="text" value="    " id="phone">
<input type="text" value="        " id="floatNum">
<input type="text" value="  html  " id="htmlTags">
<script>
    window.onload=function(){
        var number=document.getElementById("banNumber");
        clearNonumber=function(tThis){
            var _v=tThis.value;
            tThis.value=_v.replace(/\D/g,""); //     
        }
        number.onfocus=function(){
            clearNonumber(this);
        }
        number.onblur=function(){
            clearNonumber(this);
        }
        var chinese=document.getElementById("chineseStr");
        clearNochinese=function(tThis){
            var _v=tThis.value;
            tThis.value=_v.replace(/[^\u4e00-\u9fa5]/g,"");
        }
        chinese.onfocus=function(){
            clearNochinese(this);
        }
        chinese.onblur=function(){
            clearNochinese(this);
        }
        var email=document.getElementById("email");
        clearNochinese=function(tThis){
            var _v=tThis.value;
            var reg=/^\s*[a-zA-Z0-9]+(([\._\-]?)[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([_\-][a-zA-Z0-9]+)*(\.[a-zA-Z0-9]+([_\-][a-zA-Z0-9]+)*)+\s*$/;
           var isEmail=reg.test(_v);
            var span=document.createElement("span");
            if(!isEmail){
                var text=document.createTextNode("        !");
                span.appendChild(text);
                insertAfter(span,tThis);
            }
            else {
                var text=document.createTextNode("    !");
                span.appendChild(text);
                insertAfter(span,tThis);

            }

        }
        email.onblur=function(){
            clearNochinese(this);
        }
        var phone=document.getElementById("phone");
        clearNophone=function(tThis){
            var _v=tThis.value;
            var reg= /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,})){0,}$/;//  
            var isPhone=reg.test(_v);
            var span=document.createElement("span");
            if(!isPhone){
                var text=document.createTextNode("         !");
                span.appendChild(text);
                insertAfter(span,tThis);

            }
            else {
                var text=document.createTextNode("    !");
                span.appendChild(text);
                insertAfter(span,tThis);
            }
        }
        phone.onblur=function(){
            clearNophone(this);
        }
        var floatNum=document.getElementById("floatNum");
        clearNofloat=function(tThis){
            var _v=tThis.value;
            var reg=/^\d+(\.\d+)+$/;//       
            var isFloat=reg.test(_v);
            var span=document.createElement("span");
            if(isFloat){
                var text=document.createTextNode("    !");
                span.appendChild(text);
                insertAfter(span,tThis);
            }
           else {
                var text=document.createTextNode("     !");
                span.appendChild(text);
                insertAfter(span,tThis);
            }
        }
        floatNum.onblur=function(){
            clearNofloat(this);
        }

        var htmlTags=document.getElementById("htmlTags");
        clearNohtmlTags=function(tThis){
            var _v=tThis.value;
            tThis.value=_v.replace(/<[\/\!]*[^<>]*>/ig,"");//  html  
        }
        htmlTags.onblur=function(){
            clearNohtmlTags(this);
        }
    }
    function insertAfter( newElement, targetElement ){ // newElement        targetElement         
        var parent = targetElement.parentNode; //           
        if( parent.lastChild == targetElement ){ //                                appendChild  
            parent.appendChild( newElement, targetElement );
        }else{
            parent.insertBefore( newElement, targetElement.nextSibling );
        };
    };
</script>
</body>
</html>