如何获取表单提交警报以取消表单操作?

时间:2013-08-01 15:24:06

标签: forms validation action onsubmit actionform

我有一个javascript函数,可以检查是否有人输入了他们的电子邮件:

function formValidate(){

    form1 = document.forms['_register1'];

    if (form1.elements['email'].value == 'Type Your Email Address Here') 

    {

        alert('Please enter your email address.');

        form1.elements['email'].focus();

        return false;

    }
}

这是表单html:

<form method="post" enctype="multipart/form-data" name="_register1" id="_register" action="signup.php" >
<input type="text" class="field" value="Type Your Email Address Here" name="email" title="Type Your Email Address Here" />
<input type="submit" class="button button-signup" value="SIGN UP!" onclick="formValidate();" /> 

如果用户没有输入电子邮件地址,我希望弹出javascript警报,一旦用户按下确定,我希望页面保持不变。我不希望在电子邮件有效之前发生action =“signup.php”。这似乎应该很简单,但我已经浏览了整个互联网,无法找到解决方案。

感谢。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我终于找到了答案。关键是使用javascript submit()函数。我只使用常规按钮,而不是使用输入类型提交。然后,在javascript验证表单并且表单具有正确的数据后,我使用了提交功能。这是我的代码如下。如果有人好奇,请随时向我询问。

function formValidate(){

    //define form1 as registration form
    form1 = document.forms['_register1'];
    var submission = true;
    //make sure email address is not blank
    if (form1.elements['email'].value == 'Type Your Email Address Here') {

        alert('Please enter your email address.');

        form1.elements['email'].focus();

        var submission = false;
    } 
    //submit form if data is good
    if(submission != false){
        form1.submit();
    }
}

这是html:                 

    <input type="text" value="Type Your Email Address Here" name="email" title="Enter email" />

    <input type="button" value="SIGN UP!" onclick="formValidation();" />