簡體   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