繁体   English   中英

字母+数字值的正则表达式

[英]Regular Expression for Alphabets + Numeric Value

我正在研究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
*/

该网站将告诉您确切的信息。

正则表达式:

var match = /^[a-zA-Z][0-9]{3}$/.test("A456"); // match is true
var match = /^[a-zA-Z][0-9]{3}$/.test("AB456"); // match is false

http://www.regular-expressions.info/javascriptexample.html-有一个在线测试工具,您可以在其中检查其是否正常。

/ [AZ] [0-9] {3} /

如果要同时使用大写和小写字母,则/^[A-Za-z][0-9]{3}$/

否则,如果字母是大写字母,则/^[AZ][0-9]{3}$/

最小示例:

<html>
<head>
</head>
<body>
<form id="form" name="form" action="#">
  <input type="text" onkeyup=
   "document.getElementById('s').innerHTML=this.value.match(/^[a-z]\d\d\d$/i)?'Good':'Fail'" 
   />
  <span id="s">?</span>
</form>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM