javaScript对只能输入某些特殊组合字符串的验证

来源:互联网 发布:淘宝宝贝违规怎么处理 编辑:程序博客网 时间:2024/06/06 13:09

<script type="text/javaScript">   

/*********  

*only be some special char;  

*/  

function onlySomeChar(inputStr)   

{   

    if((inputStr.match(/^/d.*/d$/g) && (inputStr.search(/[^0-9,-]/g)==-1)) || inputStr.match(/^/d*$/))   

    {   

        for(var i =0;i<inputStr.toString().length;i++)   

        {   

            if(inputStr.charAt(i)=="-")   

            {   

                if(isInt(inputStr.charAt(i-1))==false || isInt(inputStr.charAt(i+1))==false)   

                   return false;   

            }   

        }   

        return  true;   

    }   

    return false;   

}   

  

/**  

*if Integer  return true    

*else return false;  

*/  

function isInt(str)   

{   

    if (str.search(/[^0-9]/g)!=-1)   

    {   

        return false;   

    }   

    return true;   

}   

  

//the function of test   

function test(strIn)   

{   

    if(onlySomeChar(strIn)==false)   

    {   

        alert(0);   

    }   

    else  

    {   

        alert(1);   

    }   

  

}   

</script>   

<input type="text"  id="test" onchange="test(this.value)">

原创粉丝点击