使用php验证正则表达式时遇到问题。
我在下面张贴了一个html表格,要求提供电话号码,车牌,街道地址,生日和社会保险号码。 (我现在只关心让电话号码和街道正常工作)
我需要使用preg_match功能来遵守以下电话号码标准:
电话号码 - 7位数或10位数
◦7位:前三位数字是一组,可以用短划线,一个或多个空格或根本没有空格分隔最后四位数字
◦10位数:前三位数字,后三位数字和最后四位数字是三个不同的组,每个组可以与其相邻组分开,没有任何内容,短划线或一个或多个空格或括号
◦eg。所有这些都是有效的 ▪(604)123-4567但不是604)123-4567而不是(604123-4567 ▪6041234567 ▪1234567 ▪123-4567 ▪1234567 ▪604-123-4567 ▪604123 4567 ▪6041234567 ▪604123456
我需要使用preg_match功能来遵守以下电话号码标准:
街道地址 - 三到五个地址后跟一个字符串,必须以“街道”字样结束 ◦eg。这些都是有效的 ▪123大街 ▪8888橡树街 ▪55555Dunsmuir Street
到目前为止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 :(得分: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]
- 仅限字母+&#39;&#34;和白色空间 - 街道名称。根据您的需要,您可以修改它