如果无效,请停止提交表单

时间:2016-12-22 14:46:06

标签: jquery

我有一个注册并同时登录的表单,但只允许特定域注册,我需要它做的是突出显示而不是console.log,更重要的是阻止表单提交,如果无效。< / p>

PHP表格

<form action="actions/register-action.php" method="POST">

    <input class="domain-first" type="text" name="email" placeholder="Email" autocomplete="off" required >

    <input type="password" name="password" placeholder="Password" required>

    <input type="submit" name="submit" value="Register & Login" class="submit-form">

</form>

JQuery的

$('.submit-form').on('click', function(){
    str = $('.domain-first').val();
    str = str.split('@').slice(1);

    var allowedDomains = [ 'domain1.co.uk', 'domain2.co.uk' ];

    if ($.inArray(str[0], allowedDomains) !== -1) {
        alert(str + ' is allowed');
    }else{
        alert('not allowed');
    }
}); 

1 个答案:

答案 0 :(得分:1)

您只需添加else{return false}

即可