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

我有一个简单的类项目,我正在进行一些表单验证.我们必须使用一些基本的正则表达式来验证输入.我在外部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),然后您就会看到错误原因.如果你在控制台中编写新的Regex(),它会告诉你没有定义Regex,如果你开始编写RegE …,它会自动为你完成RegExp.

上一篇:HTML标签天天练7--表单3[action][submit][reset]


下一篇:PHP FILTER_VALIDATE_FLOAT 过滤器