PHPの教科書12 正規表現で判定する
郵便番号の入力された値を、正規表現で判定する。
/\A\d{3}\-\d{4}\z/
http://felica.boy.jp/textbook/lecture1-2-8.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>よく分かるPHPの教科書</title> </head> <body> <form action="lecture1-2-8.php" method="get"> <dl> <dt>郵便番号(例:123-4567)</dt> <dd><input type="text" name="zip" size="10" maxlength="8" id="zip"></dd> </dl> <input type="submit" value="送信する"> </form> </body> </html>
<?php $zip = mb_convert_kana($_REQUEST['zip'], 'a', 'UTF-8'); if (preg_match("/\A\d{3}\-\d{4}\z/", $zip)) { print('郵便番号:〒' .$zip); } else { print('※郵便番号は123-4567の形式でご記入ください'); } ?>