js 验证邮箱格式

<script type="text/javascript">
        function validate_email(field, alerttxt) {
            with (field) {
                apos = value.indexOf("@")
                dotpos = value.lastIndexOf(".")
                alert(apos);
                alert(dotpos);
                if (apos < 1 || dotpos - apos < 2)
                { alert(alerttxt); return false }
                else { return true }
            }
        }

        function validate_form(thisform) {
            with (thisform) {
                if (validate_email(email, "不是有效的邮箱地址!") == false)
                { email.focus(); return false }
            }
        }
    </script>
<form action="XXX.htm" onsubmit="return validate_form(this)">
Email: <input type="text" name="email" size="30"/>
<input type="submit" value="Submit"/> 
</form>

 

posted @ 2013-01-25 14:55  dreamfly_cc  阅读(283)  评论(0编辑  收藏  举报