JavaScript RegEx.test() 有条件地中止/提前返回函数

标签 javascript regex forms submit

我有一个简单的类项目,我正在其中进行一些表单验证。我们必须使用一些基本的正则表达式来验证输入。我在外部 js 文件中有这段代码。

我有一个捕获 onsubmit 事件的函数,并返回是否继续回发的验证函数的值。

问题是当我调用 RegEx.test() 方法时,我的验证函数停止并返回,并且回发无意中进行。同样奇怪的是,如果我使用“new RegEx()”创建一个正则表达式对象,同样的事情也会发生。我不知道为什么。

这是我的代码:

$(document).ready(function() {

    // onsubmit event    
    $('form').submit(
        function() {
            return validation();
        }
        );

    // form validation
    function validation() {

        resetErrors();

        //variables
        var validates = true;
        var errorString = "";
        //var isEmail = new RegEx('^.+@.+\..+$'); // function stops here if line is uncommented and kicks off the postback?
        //alert("harro new regex?"); // does not run if above line is uncommented


        var isEmail = "/^.+@.+\..+$/"; // this doesn't stop the function
        alert("harro isEmail direct regex string assignment"); // runs

        var isZipCode = "/^(\d{5}$)|(^\d{5}-\d{4}$)/"; // this doesn't stop the function
        alert("harro isZipCode direct regex string assignment"); // runs

        //firstname
        if (!$('#firstName').val().trim()) {
            validates = false;
            $('#firstName').addClass('error');
            errorString += "First Name is blank. <br />";
        }
        //lastname
        if (!$('#lastName').val().trim()) {
            validates = false;
            $('#lastName').addClass('error');
            errorString += "Last Name is blank. <br />";
        }
        //email
        if (!$('#email').val().trim()) {
            validates = false;
            errorString += "Email is blank. <br />";
            $('#email').addClass('error');
        }
        // if this runs the test, the function stops here
        else if (!isEmail.test(email.value)) {
            validates = false;
            errorString += "Please enter a valid email address. <br />";
            $('#email').addClass('error');
        }
        alert("harro rest of function after email test?")// does not run if "else if" is ran

        //address
        if (!$('#address').val().trim()) {
            validates = false;
            $('#address').addClass('error');
            errorString += "Address is blank. <br />";
        }

        //zipcode
        if (!$('#zipCode').val().trim()) {
            validates = false;
            $('#zipCode').addClass('error');
            errorString += "Zipcode is blank. <br />";
        }
        // if this runs the test, the function stops here
        else if (!isZipCode.test(zipCode.value)) {
            validates = false;
            $('#zipCode').addClass('error');
            errorString += "Please enter a valide zip code. <br />";
        }
        alert("harro rest of function after zipcide test?")// does not run if "else if" is ran


        if (validates) {
            return true;
        }

        //output error messages
        $('#pageTitle').after("<p id='errorText'>" + errorString + "</p>");

        return false;
    }

    //reset validation errors
    function resetErrors() {

        $('#errorText').remove();
        $('#firstName').removeClass('error');
        $('#lastName').removeClass('error');
        $('#email').removeClass('error');
        $('#address').removeClass('error');
        $('#zipCode').removeClass('error');

    }

});

最佳答案

你少了一个“p”,它是 RegExp 而不是 RegEx

var isEmail = new RegExp('^.+@.+\..+$');

但一般来说,对于这些问题,只需打开 Firebug 或 Chrome 开发者工具(Windows 中的 F12),然后您就会看到错误的原因。如果你在控制台写new Regex(),它会告诉你Regex is not defined,如果你开始写RegE...,它会自动为你补全到正则表达式。

关于JavaScript RegEx.test() 有条件地中止/提前返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13034295/

相关文章:

javascript - pset8; CS50; javascript;何时使用命名函数以及何时在回调中使用匿名函数

php - 从一个域重定向到另一个域并在新的 url 末尾添加 .html

jquery - 完成 Ajax 请求后提交表单

php - 无法使用 html 和 php 更新 mysql 中的值

javascript - 错误 : <path> attribute d: Expected number, "MNaN,NaNLNaN,NaN"

javascript - MEAN.js 和 MEAN.io 的区别

regex - 如何使用 sprintf 创建一个正则表达式来匹配文本文件中的 1 个但不超过 N 个连续单词?

javascript - jQuery.form uploadProgress 从未调用过

javascript - document.getElementById ("ID").focus() + 谷歌浏览器

javascript - 如何使用 jquery regex 将字符串中的链接替换为可点击的版本?