如何验证格式的表单字段-00.000.000.0-000.000

问题描述

我正在用PHP创建一个表单,在表单中我有一个字段“ TAX ID”。该字段的值只能采用这种格式-“ 00.000.000.0-000.000”

关于如何在“ TAX ID”字段中以这种格式进行验证的任何建议?

谢谢

解决方法

我建议您使用正则表达式 https://www.w3schools.com/php/php_regex.asp

<?php
$str = "Visit W3Schools";
$pattern = "/w3schools/i";
echo preg_match($pattern,$str); //this is true
?>

例如

/[0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9]-[0-9][0-9][0-9].[0-9][0-9][0-9]/