0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

年月日入力時のバリデーション実装(PHP)

Posted at

日付入力のバリデーションを実装したときの記録

今回は年月日の入力の際のバリデーションを実装した。
入力して欲しい形式は(YYYY-MM-DD)とした。(月日は1桁でもデータベースには登録できるが今回はこの形式にした)

段階としては下記の2段階で行った

  1. 日付の形式が正しいかどうかチェック(YYYY-M-DやYYYYMMDDなどが入力されたときにエラー) 
  2. その日付が存在するかどうかチェック

実際に使用したコード

    if ((preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $internship['test_date']) == false)) {
        $errors['test_date'] = 'WEBテスト締め切り日の形式が正しくありません';
    } else {
        list($year, $month, $day) = explode('-', $internship['test_date']);

        if (checkdate($month, $day, $year) == false) {
            $errors['test_date'] = 'WEBテスト締め切り日が正しくありません';
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?