gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:50 25 4
gpt4 key购买 nike

我有一个简单的类项目,我正在其中进行一些表单验证。我们必须使用一些基本的正则表达式来验证输入。我在外部 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com