我只是想知道如果你可愛的民間能夠給我一些指針,我可能會出錯的地方。問題與正則表達式進行驗證
我已經爲英國固定電話號碼實施了一個正則表達式檢查器,除了一組數字之外,它似乎都可以工作。這是我第一次使用正則表達式。
下面是我使用它的正則表達式:
((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})|([3489]{1}\d{2}\s?\d{3})\s?\d{3})))
那是給我找麻煩該集團是
((02(((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})$)... (in the interests of brevity, I have cut out the remaining portion of it. The parentheses are all present and correct in the full regex
我已經檢查了它反對regexpal,它似乎正確驗證。
我使用的
02031111111 <- Valid
0203 111 1111 <- Valid
020 3111 1111 <- Valid
020311111111 <- Invalid (passes validation)
0203 11111111 <- Invalid (passes validation)
020 3111 11111 <- Invalid (fails validation - which is what I want)
這是其中執行
function valid_phone(landline, country)
{
var homep = '';
switch (country)
{
case 'England':
homep = /^((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4})|([3489]{1}\d{2}\s?\d{3})\s?\d{3})))$/;
break;
case 'USA':
homep = /^((((\(\d{3}\))|(\d{3}(-|)))\d{3}(-|)\d{4})|(\+?\d{2}((-|)\d{1,8}){1,5}))$/;
break
default:
homep = /^((01([2-69][1]\s?\d{3}\s?\d{4}$|[2-9][02-9][0-9]\s?\d{3}\s?\d{3}$)))|((02(((0\s?[378]\s?(\d{3}|\d{4})\s?\d{4}))|([3489]{1}\d{2}\s?\d{3})\s?\d{3}$)))$/;
}
return homep.test(landline);
}
正則表達式功能這是其中它被稱爲jQuery中
$('#landline').blur(function()
{
$lp = $('#landline').val();
$co = $('#country').val();
if (!valid_phone($lp, $co))
{
$('#error').slideDown();
$('#error').append('Wrong phone number format');
}
else
{
$('#error').slideUp();
$('#error').html('');
}
});
我的代碼塊中的測試數正如我所說,任何指針將不勝感激。我相信這很簡單。我也嘗試過沒有最後的$),它仍然是一樣的。
在此先感謝。
你能否提供更多的例子有效和無效的輸入?我想我已經發現了這個問題,但我不確定預期的行爲是什麼。 –
那麼意外的失敗測試用例最後有五個1,但是正則表達式在末尾說'\ d {4}'(意思是「正好4位數」)。我錯過了明顯的東西嗎? – bishop
@KrzysztofKosiński就像主教所說的那樣。我需要在最後有4個數字。 – Chris