[英]Need help for a regular expressions with PHP Preg_match to validates a number field on our form
:)我們想設置一個特殊條件(基於PHP Preg_match正則表達式)來驗證表單上的數字。
首先,“數字字段”最多只能包含13個數字(並且只能包含數字。不能包含字母或其他內容)。
第一個數字必須是(僅)“ 1”或“ 2”(沒有其他內容)
第4和第5個數字代表(組合的2個數字)某人的“出生月份”,因此第4個數字必須為“ 0”或“ 1”,第5個數字必須介於“ 1”和“ 9”之間。
非常感謝您能為我們提供幫助,在PHP Preg_match中為正則表達式提供良好的“語法”,以在我們的表單上驗證該字段! :)
感謝社區的支持和幫助!
問候
這是您描述給我們的文字正則表達式模式:
^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$
示例腳本:
$input = "1231212345678";
if (preg_match("/^[12]\d{2}(?:0[1-9]|1[0-2])\d{8}$/", $input)) {
echo "MATCH";
}
此正則表達式模式表明:
^ from the start of the string
[12] match 1 or 2 as the first digit
\d{2} then match any digits in the 2nd and 3rd position
(?:0[1-9]|1[0-2]) match 01, 02, ..., 12 as the two digit month
\d{8} then match any other 8 digits
$ end of string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.