gpt4 book ai didi

jquery - 根据数组值验证表单

转载 作者:行者123 更新时间:2023-12-01 05:46:21 26 4
gpt4 key购买 nike

我对 jQuery 和一般编码很陌生,想知道是否有人可以帮助我。

我有两个输入字段(#cp1 和 #cp2)、一个提交按钮(#enter)和两个数组(arr1 和 arr2)。

arr1 = ["1700", "1000", "1749"];

arr2 = ["001", "002", "003"];

#cp1<​​ 应该是:仅限数字;最多 4 位数字;最少 4 位数字;仅当其值等于 arr1 中的值时才进行验证。

#cp2 应该是:仅限数字;最多 3 位数字;最少 3 位数字;仅当其值等于 arr2 中的值时才进行验证。

按下#enter 后,它应该检查#cp1 和#cp2 是否都为真,并返回适当的警报消息。

这是我的 HTML:

<input id="cp1" type="text" maxlength="4" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input> 
<input id="cp2" type="text" maxlength="3" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>
<button type="submit" id="enter">Confirmar</button>

如果每个输入只有一个变量,我就能得到正确的结果,但我不知道如何让它在数组中搜索正确的值。

提前致谢!

最佳答案

使用 jQuery (Fiddle):

arr1 = ["1700", "1000", "1749"];

arr2 = ["001", "002", "003"];

$(function() {
$("#enter").click(function() {
if(jQuery.inArray($("#cp1").val(), arr1) == -1)
alert('Value not in first array');
if(jQuery.inArray($("#cp2").val(), arr2) == -1)
alert('Value not in second array');
});
});

关于jquery - 根据数组值验证表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429032/

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