2012-06-22 63 views
9

我有一個用JavaScript編寫的腳本來驗證加拿大郵政編碼使用正則表達式,但它似乎並沒有工作。下面是該腳本:驗證加拿大郵政編碼正則表達式

如果聲明:

if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length < 12) { 
    alert("Please fill in field Postal Code. You should only enter 7 characters"); 
    myform.zip.focus(); 
    return false; 
    } 

功能:

function okNumber(myform) { 
    var regex = /^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$/; 
    if (regex.test(myform.zip.value) == false) { 
    alert("Input Valid Postal Code"); 
    myform.zip.focus(); 
    return false; 
    } 

    return true; 
} 
+0

「{1}」業務毫無意義。無論如何,一個'[]'字符類已經是一個隱式的'{1}',與'\ d'和任何與單個字符相匹配的東西一樣。除此之外,這不起作用?你是否檢查過輸入沒有前/後空格? –

+0

什麼是輸入和輸出的例子?或者根本不起作用 – Charlie

+2

有效的加拿大郵政編碼的規則是什麼?這個正則表達式失敗了什麼測試輸入? –

回答

3

一個正則表達式的方法可以驗證format of a Canadian postcode,但它不足以保證郵政編碼實際上存在

例如:A9A 0A0看起來像一個有效的加拿大郵政編碼,但前分揀區A9Adoesn't actually exist

你確定你不想對官方的郵政編碼列表進行某種查詢嗎?

+0

我只需要驗證格式。 –

+2

在這種情況下,像'^ [A-Z] \ d [A-Z] []?\ d [A-Z] \ d $'[將工作](http://regexr.com?31b0q)。如果這個正則表達式在你的程序中不起作用,你可能有邏輯錯誤(true而不是false?),錯誤地調用regex代碼,或者根本不調用它。 –

+0

這不起作用。試試http://depressionnovascotia.com/上的表格 –

0

第一個錯誤是您最初的if語句中的最後一個條件。 myform.zip.value.length < 12應始終爲真,因此代碼的這部分將始終提醒消息"Please fill in field Postal Code. You should only enter 7 characters"並將焦點恢復到zip字段。由於有效的郵政編碼最多有7個字符,因此應將其更改爲myform.zip.value.length > 7

做出更正後,您在註釋中提供的郵政編碼T2X 1V4將生效。但是,您使用的正則表達式可以被簡化(正如在評論中也提到的那樣)。您可以刪除{1}的所有實例,因爲它們是多餘的。你可能也想用?代替*。 A ?表示前一個字符或表達式可以出現0或1次,而*表示它可以出現0或更多次。我想你最多隻需要一個郵政編碼空間。

下面是我測試過的全部工作代碼:

<!doctype html> 
<html> 
<head> 
    <title>JavaScript Regex Tester</title> 
    <meta charset="utf-8"> 
    <script> 
     function validate(myform) { 
      if (myform.zip.value == "" || myform.zip.value == null || myform.zip.value == "Postal Code" || myform.zip.value.length > 7) { 
       alert("Please fill in field Postal Code. You should only enter 7 characters"); 
       myform.zip.focus(); 
       return false; 
      } 

      return okNumber(myform); 
     } 

     function okNumber(myform) { 
      var regex = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] *\d[A-Z]\d$/; 
      if (regex.test(myform.zip.value) == false) { 
       alert("Input Valid Postal Code"); 
       myform.zip.focus(); 
       return false; 
      } 

      return true; 
     } 
    </script> 
</head> 
<body> 
    <form action="#" name="myform" method="post"> 
     <input type="text" name="zip" value="Postal Code" /> 
     <input type="button" value="Submit" onclick="validate(document.myform);"/> 
    </form> 
</body> 
</html> 

最後要說明的,通常當你在正則表達式看到[A-Z]它的價值至少在考慮是否應該[A-Za-z]接受的上限或小寫字母。我不知道加拿大郵政編碼是否屬於這種情況,但通常情況下,大多數表格都應接受任何輸入並根據需要進行糾正。

4

這將爲所有加拿大郵政編碼工作..

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$ 
2

下面是規則 http://en.wikipedia.org/wiki/Postal_code#Reserved_characters

ABCEGHJKLMNPRSTVXY <-- letter used 
DFIOQU <-- letters not used because it mixes up the reader 
WZ  <-- letters used but not in the first letter 
With that in mind the following in the proper regex 

@[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][\s][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9] 
0

這是我使用一個加拿大郵政編碼有效的表達。 它將格式嚴格爲A0A 0A0。

/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/ 
0

這將爲加拿大郵政編碼做到:

/^[a-z][0-9][a-z][- ]?[0-9][a-z][0-9]$/i 

這將允許用空格或連字符正確的X#X#X#格式。

相關問題