2016-11-04 8 views
0

PHPを使って正規表現を検証する際に問題があります。PHP正規表現の問題

私は、電話番号、ナンバープレート、住所、誕生日、社会保険番号を尋ねるhtmlフォームを掲載しています。 (私は今のところ正常に動作するように電話番号と通りを取得して唯一の心配)

私は電話番号の次の基準に従うことにするpreg_match関数を使用する必要があります。

電話番号 - 7桁または10桁

◦7桁:最初の3桁が一つのグループであり、全て

◦10数字にダッシュ、1つまたは複数のスペース、または何も最終的な4桁から分離することができます:fi最初の3桁、2番目の3桁、最後の4桁は3つの異なるグループであり、各グループは何もないか、ダッシュ、1つ以上のスペース、かっこで区切られています。

◦eg。これらのすべては604123から4567 ▪6041234567 ▪1234567 ▪123-4567 ▪1234567 ▪604-123-4567 ( ▪(604)123-4567ではなく604)123-4567なく有効である▪ 604 123 4567 ▪6041234567 ▪604123456

私は電話番号については、次の基準に従うことにするpreg_match関数を使用する必要があります。

住所 - 文字列が続く3つから5つの番号アドレス、 "Street"という語で終わらなければならない ◦eg。これらは ▪123メインストリート ▪8888オークストリート ▪55555ダンスミアストリートこれまでlab11.htmlため

コード有効であり、lab11.php lab11.html

<!DOCTYPE html> 
<html> 
<head> 
<title>Lab 11</title> 
<meta charset="utf-8"> 
</head> 
<body> 
<form action="lab11.php" method="POST"> 
<input type="text" name="phoneNumber"placeholder="Phone Number" style="font-size: 15pt"> 
<br> 
<input type="text"name="licensePlate"placeholder="License Plate" style="font-size: 15pt"> 
<br> 
<input type="text" name="streetAddress" placeholder="Street Address" style="font-size: 15pt"> 
<br> 
<input type="text" name="birthday" placeholder="Birthday" style="font-size: 15pt"> 
<br> 
<input type="text" name="socialInsuranceNumber" placeholder="Social Insurance Number" style="font-size: 15pt"> 
<br> 
      <input type="submit" name="submit" value="Submit"> 
</form> 
</body> 
</html> 

lab11.php

<?php 
    // Get phone number, license plate, street address, birthday and 
    // social insurance number entered from lab11.html form 
    $phoneNumber = $_POST['phoneNumber']; 
     echo "Your phone number is " . $phoneNumber; 
     echo "<br>"; 
    $licensePlate = $_POST['licensePlate']; 
     echo "Your License Plate Number is " . $licensePlate; 
     echo "<br>"; 
    $streetAddress = $_POST['streetAddress']; 
     echo "Your Street Address is " . $streetAddress; 
     echo "<br>"; 
    $birthday = $_POST['birthday']; 
     echo "Your Birthday is " . $birthday; 
     echo "<br>"; 
    $socialInsuranceNumber = $_POST['socialInsuranceNumber']; 
     echo "Your Social Insurance Number is " . $socialInsuranceNumber; 

    echo "<br>"; 

    // Validate regular expression for phone number entered 
    if (preg_match("/^\(.[0-9]{3}[0-9]$/", $phoneNumber)) { 
     echo "Your phone is correct."; 
    } 
    else { 
     echo "Your password is wrong."; 
    } 
    // Validate regular expression for license plate entered 
    if (preg_match("/{3,5}.String$/", $streetAddress)) { 
     echo "Your plate is correct."; 
    } 
    else { 
     echo "Your plate is wrong."; 
    } 
?> 

答えて

0

電話番号の正規表現:

^(\([\d]{3}\)|[\d]{3})?(-|\s*)?([\d]{3})?(-|\s*)?[\d]{3}(-|\s*)?[\d]{4}$ 

とアドレス用:

^([\d]{3,5})\s+[a-zA-Z'"\s]+\s*Street$ - you can add `i` modifier for case insensitive 

[a-zA-Z'"\s]は - 通りの名前のために - 文字のみ+「」とホワイトスペースを与えられました。あなたのニーズに応じてそれを修正することができます

+0

これは有効なアドレスとして '++++ _______ Street'を受け入れます!有効な電話番号として「++++++++++++++」 – Toto

+0

コメントありがとうございます!私は間違いを修正した。 – krasipenkov