2013-02-11 37 views
0

無論我在StreetAddress中輸入什麼內容,都無法匹配正則表達式。無論是PO Box PO BOX po box。我似乎無法得到這個工作。有任何想法嗎?無法從文本框中獲取文本以匹配正則表達式

function valPoBox(sender, args) { 
     var hasPObox = new RegExp('\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b'); 
     var StreetAddress = $('.streetaddress').val(); 

     if (StreetAddress.match(hasPObox)) { 
      args.IsValid = false; 

      sender.ErrorMessage = "Address must not contain P.O. Box"; 
//   $('.valPoBox').attr("ErrorMessage", sender.ErrorMessage); 
     } 
     else { 
      args.IsValid = true; 
     } 
     $('.valPoBox').attr("errormessage", sender.ErrorMessage); 
    } 
+0

置換變種hasPObox =新正則表達式('\ B [P | P] *(OST | OST)* \ * \ S *〔O | O | 0]。*(辦公室支持|辦公室支持)* \ * \ S * [b | b]。【O | O | 0] [X | X] \ b')的; 與: var hasPObox = /^[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffICE|FFICE)*\.*\s*[B | b]〔O | O | 0] [X | X] \ S *(\ d)*/GI; 它現在正在工作。對不起,夥計! – reds184 2013-02-11 18:47:18

+1

'i'國旗忽略了案件;你並不需要所有那些上面的東西。 – Mathletics 2013-02-11 18:48:31

回答

0

整個正則表達式是太複雜:

var poBoxRegex = /po?(st)? *(office)? box/gi; 

結果:

"34 po box 333".match(poBoxRegex); 
["po box"] 
"34 postoffice box 333".match(poBoxRegex); 
["postoffice box"] 
"34 post office box 333".match(poBoxRegex); 
["post office box"] 
+0

無法獲取屬性'匹配'的值:對象爲空或未定義 – reds184 2013-02-11 19:42:12

+0

我沒有清理足夠的代碼 - 現在好了 – David 2013-02-11 21:25:13

相關問題