gpt4 book ai didi

javascript - 字母正则表达式 + 数值

转载 作者:行者123 更新时间:2023-11-28 04:02:14 28 4
gpt4 key购买 nike

我正在开发一个 Java 脚本,为此我需要正则表达式来检查文本框中输入的文本是否应该是字母和数值的组合。

我尝试了 Java 脚本的 NaN 函数,但字符串的最小长度和最大长度应为 4,并以字母作为第一个元素,其余 3 个元素应为数字。

例如:A123、D456、a564 的正则表达式而不是 (AS23、1234、HJI1)

请建议我!!!

代码在这里:

<script type="text/javascript">
var textcode = document.form1.code.value;
function fourdigitcheck(){
var textcode = document.form1.code.value;
alert("textcode:"+textcode);
var vmatch = /^[a-zA-Z]\d{3}$/.test("textcode");
alert("match:"+vmatch);
}
</script>
<form name="form1">
Enter your Number <input type="text" name="code" id="code" onblur="fourdigitcheck()" />
</form>

最佳答案

^[A-Z]{1}\d{3}$

或更短

^[A-Z]\d{3}$

描述:

// ^[A-Z]\d{3}$
//
// Assert position at the start of the string or after a line break character «^»
// Match a single character in the range between "A" and "Z" «[A-Z]»
// Match a single digit 0..9 «\d{3}»
// Exactly 3 times «{3}»
// Assert position at the end of the string or before a line break character «$»

测试:

/*
A123 -> true
D456 -> true
AS23 -> false
1234 -> false
HJI1 -> false
AB456 -> false
*/

关于javascript - 字母正则表达式 + 数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3501896/

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