Javascript 表单验证需要输入才能继续

Javascript 表单验证需要输入才能继续,javascript,validation,Javascript,Validation,一些简单的表单验证似乎需要在添加正确的值后按Enter键,然后允许访问者继续。有没有办法消除这种情况?下面是其中一个例子 // Makes sure that the email looks valid and contains an @, a . and at least two characters after the dot function checkEMail(obj) { var emailFilter = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|

一些简单的表单验证似乎需要在添加正确的值后按Enter键,然后允许访问者继续。有没有办法消除这种情况?下面是其中一个例子

// Makes sure that the email looks valid and contains an @, a . and at least two characters after the dot
function checkEMail(obj) {
    var emailFilter =  /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ ;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    if (!emailFilter.test(obj)) {
        obj.style.background = 'Yellow';
        alert('Please enter a valid email address, then press Enter to continue.');
    } else if (!illegalChars.test(obj)) {
        obj.style.background = 'Yellow';
        alert('The email address contains illegal characters.');
    } else {
        obj.style.background = 'White';
    }
}
//确保电子邮件看起来有效并且包含@,a。点后至少有两个字符
功能检查电子邮件(obj){
var emailFilter=/^[a-zA-Z0-9.!$%&'*+/=?^{124}-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])(?:\[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])*/;
变量illegalChars=/[\(\)\\,\;\:\\\\“\[\]]/;
如果(!emailFilter.test(obj)){
obj.style.background='黄色';
警报('请输入有效的电子邮件地址,然后按enter继续');
}否则如果(!非法字符测试(obj)){
obj.style.background='黄色';
警报('电子邮件地址包含非法字符');
}否则{
obj.style.background='白色';
}
}

只需在验证功能的“快乐流”中返回到
true

// Makes sure that the email looks valid and contains an @, a . and at least two characters after the dot
function checkEMail(obj) {
    var emailFilter =  /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ ;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    if (!emailFilter.test(obj)) {
        obj.style.background = 'Yellow';
        alert('Please enter a valid email address, then press Enter to continue.');
    } else if (!illegalChars.test(obj)) {
        obj.style.background = 'Yellow';
        alert('The email address contains illegal characters.');
    } else {
        obj.style.background = 'White';
        return true;
    }
}

这听起来和答案一模一样,我确信这是我错过的非常简单和明显的东西。我是一个后端程序员,对Javascript知之甚少,但可以拼凑简单的东西。然而,我注意到这个领域似乎没有达到它的“快乐流”“状态,即使在添加return true后仍保持黄色;它需要输入才能继续。这是因为一些if条件正在完成并输入到它们的流中,显示警报,然后,您需要点击输入才能继续。我添加了一个类似的逻辑脚本来帮助澄清这一点。那么我该如何应对呢?按Enter键是不实际的,因此可能正则表达式本身是错误的。即使第一次正确填充该字段,它也是黄色的,会发出警报并要求Enter继续。我使用onchange=“checkEMail(this);”从表单字段调用它。因此,也许这不是正确的方法?如果您确信您尝试验证的电子邮件是正确的,我会检查正则表达式。它似乎在所有函数上都有相同的问题,有些函数的正则表达式非常简单。