gpt4 book ai didi

javascript - jquery 在 .on 内运行条件验证

转载 作者:行者123 更新时间:2023-12-03 03:58:42 27 4
gpt4 key购买 nike

我有一个 ID 为 #primarySearch 的表单。它有 3 个文本输入,所有这些都有自己的 ID,如下所示:#ecNumber#casNumber#substanceName

我有以下js。如果用户在 #primarySearch 中的文本输入中输入任何内容,它将运行一个名为 processPrimarySearch 的函数并向其发送适当的输入值:

$('#primarySearch input[type="text"]').on({
"keyup": function(e) {
// Ignore tab key
if (e.which != 9) {
processPrimarySearch.call(this);
}
}
});

function processPrimarySearch() {
// ...
}

我还有一些其他 js(位于 document.ready 内),可以阻止用户输入除数字和破折号之外的任何内容 - 但仅限于 #ecNumber code> 和 #casNumber 字段(请注意,我改编自 jQuery only allow numbers,letters and hyphens )。尽管如果用户在这两个字段中输入内容,则会触发此代码,但无论用户输入是否有效,它也会导致 processPrimarySearch 运行。这是因为上面的代码和下面的代码之间没有联系:

$('#ecNumber, #casNumber').keypress(function (e) {
var allowedChars = new RegExp("^[0-9\-]+$");
var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (allowedChars.test(str)) {
return true;
}
e.preventDefault();
return false;
}).keyup(function() {
// the addition, which will check the value after a keyup (triggered by Ctrl+V)
// We take the same regex as for allowedChars, but we add ^ after the first bracket : it means "all character BUT these"
var forbiddenChars = new RegExp("[^0-9\-]", 'g');
if (forbiddenChars.test($(this).val())) {
$(this).val($(this).val().replace(forbiddenChars, ''));
}
});

目前发生的结果是,如果在 #ecNumber 中输入了诸如“z”之类的字符,验证正则表达式代码将触发并阻止字符“z”出现在输入-好。但是,processPrimarySearch() 也会触发,因为它是在 .on 中为 #primarySearch 表单中的任何输入定义的。

我的问题:我想要做的是在我的 .on 中运行验证正则表达式,但前提是它是 #ecNumber#casNumber 字段( #substanceName 不得在此处验证)。

我已经成功编写了以下内容,它使用数组来说明用户正在哪个字段中输入内容。我正在执行 console.log 的地方是我需要验证正则表达式的地方

 $('#primarySearch input[type="text"]').on({
"keyup": function(e) {
// Ignore tab key
if (e.which != 9) {

var arr = ["ecNumber", "casNumber"];
if ($.inArray($(this).attr('id'), arr)!== -1) {
console.log($(this).attr('id'));
}

processPrimarySearch.call(this);
}
}
});

最好的方法是什么?我不确定是否将执行正则表达式的代码移至函数中,然后调用它(使用 .call?)或其他方法?这本质上是异步的,有什么问题吗?

最佳答案

这一切都与程序流程以及您希望如何处理它有关。您的问题实际上并不是您提供代码的最后一种方式的异步问题。这是一种解决方案(许多可能的解决方案)。我在代码中添加了一些注释来解释基础知识。

$('#primarySearch input[type="text"]').on({
"keyup": function(e) {
// if this is not the tab key and the input contains valid characters
if (e.which != 9 && isThisValid($(this), e)) {
processPrimarySearch.call(this);
}
else {
e.preventDefault();
}
}
});


function isThisValid($elem, event) {
var ids = ["ecNumber", "casNumber"], // ids to validate, everything else is valid
result = true; // default result

// is this one of the id's to validate?
if(ids.indexOf($elem.attr('id')) !== -1) {
// do some validation
var allowedChars = new RegExp("^[0-9\-]+$"),
str = String.fromCharCode(!event.charCode ? event.which : event.charCode);

// is it valid?
if (!allowedChars.test(str)) {
result = false;
}
}

return result;
}

function processPrimarySearch() {
// ...
}

关于javascript - jquery 在 .on 内运行条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44826389/

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