【问题标题】:jQuery - Check certain fields are filled in then validate emailjQuery - 检查某些字段是否已填写,然后验证电子邮件
【发布时间】:2013-02-20 18:31:48
【问题描述】:

刚刚尝试使用 jQuery 将两个函数拼凑在一起。在表单上提交时,代码会检查是否所有字段都已填写,如果所有内容都正确,它会转到另一个函数来检查电子邮件验证。

我想我已经接近了,因为当我按下提交时,验证警报会显示一毫秒然后它提交(我不希望它这样做,因为使用的电子邮件无效)。我很确定它的代码足够简单,因此我远离任何表单验证。

代码如下:

<script type="text/javascript"> 
    $("#test").submit(function(){
        $(".error").hide();

        var isFormValid = true;

        $(".required").each(function(){
            if ($.trim($(this).val()).length == 0){
                $(this).addClass("highlight");
                isFormValid = false;
            }
            else{
                $(this).removeClass("highlight");
            }
        });
        if (!isFormValid) { 
            document.getElementById("error").innerHTML = "You must fill in all of the fields.";
            return isFormValid;
        }
        else {
            myFunction();
        }
    });

    function myFunction() {
        var isEmailValid = true;

        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var emailaddressVal = $("#email").val();

        if (!emailReg.test(emailaddressVal)) {
            $("#email").after('<span class="error">Enter a valid email address.</span>');
            isEmailValid = false;
        }
        else {
            ///ajax post function here///
        }

        if(!isEmailValid) {
            document.getElementById("error").innerHTML = "Please enter a valid email address.";
            return isEmailValid;
        }
    }
</script>

【问题讨论】:

  • 如果在 else 块中对 myFunction() 的调用前面加上 return 会发生什么?

标签: jquery forms email-validation


【解决方案1】:

试试这个:

<script type="text/javascript"> 
$("#test").submit(function(){
    $(".error").hide();

    var isFormValid = true;

    $(".required").each(function(){
        if ($.trim($(this).val()).length == 0){
            $(this).addClass("highlight");
            isFormValid = false;
        }
        else{
            $(this).removeClass("highlight");
        }
    });
    if (!isFormValid) { 
        document.getElementById("error").innerHTML = "You must fill in all of the fields.";
        return isFormValid;
    }
    else {
        return myFunction();//should return true or false
    }
});

function myFunction() {
    var isEmailValid = true;

    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var emailaddressVal = $("#email").val();

    if (!emailReg.test(emailaddressVal)) {
        $("#email").after('<span class="error">Enter a valid email address.</span>');
        isEmailValid = false;
    }
    else {
        ///ajax post function here///
    }

    if(!isEmailValid) {
        document.getElementById("error").innerHTML = "Please enter a valid email address.";
        return isEmailValid;
    }
    return isEmailValid;//should return true or false
}
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多