在提交表单上验证电话格式

时间:2016-04-06 21:42:20

标签: javascript

我想验证电话号码输入的值以匹配此格式 (555)5555-555

我正在使用这个脚本,但是点击img

并没有做任何事情
function submit() {
            var phone = $('#number').val();
            if(!phone.match(/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/) || phone == "") {
                $("#phoneNumberInvalid").show();
                return false;
            } else {
                document.forms["phoneForm"].submit();
                return true;
            }
        } 

形式:

        <form name="phoneForm"  method="POST" onsubmit="return submit();">

<div class="continueDiv">
    <img id="continu" src="web/static/media/images/continue.jpg"
        onclick="submit();" />
</div>
    <div id="phoneNumberInvalid"
            style="position: absolute; top: 100px; left: 50px; z-index:1; display:none;"
            class="invalid">Enter a valid phone number.</div>
<div id="phoneNumberDiv"
            style="position: absolute; left: 50px; top: 220px; width: 550px; height: 112px; z-index: 4; vertical-align: middle;">
            <img src="web/static/media/images/phoneLogo.gif">
            &nbsp;&nbsp;&nbsp; <input id="number" name="number" type="text" class="phoneNumClass" />
        </div>

</form>

表单不使用Js代码验证表单 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

更改功能名称为所有浏览器修复了它!谢谢Observer!

function validate() {
                var phone = $('#number').val();
                if(!phone.match(/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/) || phone == "") {
                    $("#phoneNumberInvalid").show();
                    return false;
                } else {
                    document.forms["phoneForm"].submit();
                    return true;
                }
            }