提交表单验证不起作用

时间:2016-02-20 20:46:32

标签: javascript php html forms

嗨所以我正在阅读如何验证html表单,我的所有验证器客户端都在使用woth模式和类型。问题是,当我按提交javascript验证不运行。有我的代码:

<script language="javascript">

function validateForm()
{
    var xa = document.forms["regform"]["password"].value;
    var xb = document.forms["regform"]["password2"].value;
    var xc = document.forms["regform"]["email"].value;
    var xd = document.forms["regform"]["email2"].value;
    if (xa == xb && xc == xd){
        return true; }
    else{ return false; alert("Please enter a valid captcha code");}
}

$(document).ready(function(e) {
    try {
    $("body select").msDropDown();
    } catch(e) {
    alert(e.message);
    }
    });

</script>

他们的形式:

<form name="regform" onsubmit="return validateForm();" action="actions/register_acc.php" method="post">

<input type="password" name="password" class="input-style" required="required">
<input type="password2" name="password" class="input-style" required="required">

<input name="email" class="input-style" placeholder="your@email.com" required="required" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
<input name="email2" class="input-style" placeholder="your@email.com" required="required" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">

<input type="submit" value="ok">

</form>

在表格中我也有这些:

<select name="selectname" id="webmenu">
<option value="1">1</option>
<option value="2">2</option>
</select>

在头脑中这些:

<script src="js/msdropdown/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script src="js/msdropdown/jquery.dd.min.js" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:1)

问题在于validateForm方法本身,特别是在else块中。您在false来电之前返回alert。交换两个电话,您应该会看到alert消息。

为清楚起见,我会更改警告框中的消息,因为它与您正在验证的字段没有直接关系。

function validateForm()
{
  var xa = document.forms["regform"]["password"].value;
  var xb = document.forms["regform"]["password2"].value;
  var xc = document.forms["regform"]["email"].value;
  var xd = document.forms["regform"]["email2"].value;
  if (xa == xb && xc == xd){
    return true; 
  }
  else { 
    alert("Please enter a valid captcha code");
    return false;
  }
}

请参阅 Fiddle

相关问题