电子邮件地址验证但创建帐户,但电子邮件地址无效

时间:2015-04-02 05:31:41

标签: javascript

我编写了用于电子邮件地址验证的脚本,如果发出无效的电子邮件,它会发出警报通知。虽然帐户是使用相同的无效电子邮件地址创建并存储在数据库中...请帮助 代码似乎是:

    <div class="form">

    <ul class="tab-group">
    <li class="tab "><a href="#signup">Sign Up</a></li>
    </ul>

    <div class="tab-content">


    <form name="form1" action="trynow" method="post" onsubmit="ValidateEmail(document.form1.txtEmail)">
        <div>
            <input type="hidden" name="register_csrf" autocomplete="false" autofocus="false" value="<%=((String)request.getAttribute("register_csrf")!=null)?request.getAttribute("register_csrf"):"" %>"/>

            <% String error_msg = (String)request.getAttribute("error_msg");
                if(error_msg != null)
                {
            %>
            <div  class="alert alert-error"> <%= error_msg %></div>

            <% } %>
            <%= ((String)request.getAttribute("succ_msg") != null)?request.getAttribute("succ_msg"):"" %>

        </div>



            <div class="field-wrap">
                <label>
                    Full Name<span class="req">*</span>
                </label>
                <input required="required" name="txtName"  type="text" >
            </div>



            <div class="field-wrap">
                <label>
                    Email Address<span class="req">*</span>
                </label>
                <input required="required" name="txtEmail" type="text" >
            </div>

            <div class="field-wrap">
                <label>
                    Set A Password<span class="req">*</span>
                </label>
                <input name="txtPassword"  type="password" >
            </div>

            <div class="field-wrap">
                <label>
                    Retype Password<span class="req">*</span>
                </label>
                <input name="txtRePassword"  type="password" >
            </div>
            <div>
            <button type="submit" class="button button-block" />Get Started</button>
            </div>
        <a class="small-message" href="login"><small>Already Registered ?</small></a><a href="/"> <small> <br>Back To Home</small></a>
       </form>
        <script>
            function ValidateEmail(inputText)
            {
                var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
                if(inputText.value.match(mailformat))
                {
                    document.form1.txtEmail.focus();
                    return true;
                }
                else
                {
                    alert("You have entered an invalid email address!");
                    document.form1.text1.focus();
                    return false;
                }
            }
        </script>

    </div><!-- tab-content -->

</div> <!-- /form -->

3 个答案:

答案 0 :(得分:1)

在您提交的表单中,使用return ValidateEmail(this.txtEmail)"

<form name="form1" action="trynow" method="post" onsubmit="return ValidateEmail(this.txtEmail)">

现在,只有当email有效时,才会发生表单提交。否则它将被阻止。

此外,您引用了document.form1.text1.focus();,而text1根本不存在于表单中。所以你也必须在这里收到错误。

答案 1 :(得分:0)

您有几个问题,包括在电子邮件错误时不关注正确的字段,并在没有错误时关注某些内容。 我也会使用测试而不是匹配。

我建议改为这个(在页面的头部):

&#13;
&#13;
function ValidateEmail(inputText) {
  var val = inputText.value;
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (!mailformat.test(val)) {
    alert("You have entered an invalid email address!");
    inputText.focus();
    return false;
  }
}
window.onload=function() {
  document.getElementsByName("form1")[0].onsubmit=function() {
   return ValidateEmail(this.txtEmail);
  }
}
&#13;
<div class="form">
    <ul class="tab-group">
        <li class="tab "><a href="#signup">Sign Up</a>

        </li>
    </ul>
    <div class="tab-content">
        <form name="form1" action="trynow" method="post">
            <div>
                <div class="field-wrap">
                    <label>Full Name<span class="req">*</span>

                    </label>
                    <input required="required" name="txtName" type="text">
                </div>
                <div class="field-wrap">
                    <label>Email Address<span class="req">*</span>

                    </label>
                    <input required="required" name="txtEmail" type="text">
                </div>
                <div class="field-wrap">
                    <label>Set A Password<span class="req">*</span>

                    </label>
                    <input name="txtPassword" type="password">
                </div>
                <div class="field-wrap">
                    <label>Retype Password<span class="req">*</span>

                    </label>
                    <input name="txtRePassword" type="password">
                </div>
                <div>
                    <button type="submit" class="button button-block" />Get Started</button>
                </div> <a class="small-message" href="login"><small>Already Registered ?</small></a><a href="/"> <small> <br>Back To Home</small></a>

        </form>
        </div>
        <!-- tab-content -->
    </div>
    <!-- /form -->
&#13;
&#13;
&#13;

答案 2 :(得分:0)

而不是 text1 txtEmail

   <script>
                function ValidateEmail(inputText)
                {
                    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
                    if(inputText.value.match(mailformat))
                    {
                        document.form1.txtEmail.focus();
                        return true;
                    }
                    else
                    {
                        alert("You have entered an invalid email address!");
                        document.form1.txtEmail.focus();
                        return false;
                    }
                }
            </script>