日期:2014-05-16  浏览次数:20271 次

一个简单的问题
获取一个文本框的值,值的长度为2,第一字符只能为字母,第二字符只能数字,用js怎么判断,谢谢!

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>    
        <style>
            body {font-size:12px;}
        </style>        
    </head>
    <body>
        <input id="t" />
        <button id="btn">匹配吗?</div>
        <script>
            function $(el){
                return typeof el == 'string' ? document.getElementById(el) : el;
            }
            $('btn').onclick = function(){
                var s = $('t').value;
                var re = /^[a-z]\d$/i;
                if(re.test(s)){
                    alert('匹配')
                }else{
                    alert('不匹配')
                }
            }
        </script>
    </body>
</html>

------解决方案--------------------
<script>
function f()
{
var regex = /^[a-z]\d$/i
var v = document.getElementById('i').value;
if(regex.test(v)) alert('正确')
else alert('错误')
}

</script>
<input id='i'/>
<input type='button' value='BTN' onclick='f()'/>