半角数字に直して数字であるかをチェックする
《HTML》
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title>半角数字に直して、数字であるかをチェックする</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="wrap"> <div id="head"> <h1>半角数字に直して、数字であるかをチェックする</h1> </div> <div id="content"> <form action="sample13.php" method="get"> <dl> <dt>年齢(数字でご記入ください)</dt> <dd><input type="text" name="age" size="5" maxlength="3">歳</dd> </dl> <input type="submit" value="送信する"> </form> </div> </div> </body> </html>
《PHP》
<?php ini_set('display_errors', 'on'); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> <title>半角数字に直して、数字であるかをチェックする</title> </head> <body> <div id="wrap"> <div id="head"> <h1>半角数字に直して、数字であるかをチェックする</h1> </div> <div id="content"> <p style="margin-top: 200px"> <?php $age = mb_convert_kana($_REQUEST['age'], 'n', 'UTF-8'); if (is_numeric($age)) { print($age . '歳'); } else { print('※年齢は数字でご記入ください'); } ?> </p> </div> </div> </body> </html>