【问题标题】:Validate id number in php在php中验证id号
【发布时间】:2015-04-22 07:46:23
【问题描述】:

html格式的形式如下图:

 <tr style="height:15px;"><td></td></tr>
 <tr>
   <td>ID Card No<br /></td>
 </tr>
 <tr>
 <td><input class="input" style="width:300" size="20" type="text" name="id" maxlength="5"/></td>
 </tr>

如何以 PHP 格式验证 ID 号(例如 A1234)?

【问题讨论】:

  • “以 PHP 格式验证”是什么意思?你想验证它是什么?电话号码?电子邮件?验证的标准是什么?
  • 需要有关您的问题的更多信息?就像您尝试过的以及您到底想要什么。
  • 请详细说明您的问题..如何验证 id 字段..
  • 1.有验证规则(@Epodax 评论) 2. 使用 regular expressions 应用这些规则。
  • 在输入标签内使用 pattern="[A-Z]{1}\d{4}",但这是 html5 验证。

标签: php regex validation


【解决方案1】:

接受one upper char and four digits的简单正则表达式应该是:

$str = 'A1234';
$result = preg_match('~^[A-Z]\d{4}$~', $str);
print_r($result); // true | false

【讨论】:

  • 我不知道表达式“$str = 'A1234';”意思
  • @user3056561:这是您给我们提供的输入的价值。实际上它将是$_POST['id']$_GET['id'],取决于表单的方法属性(&lt;input name=id ...&gt; 的值)。第一行将是$str = $_GET/POST['id']
  • 正则表达式对我不起作用。有没有其他表达方式可供参考?
  • @user3056561:它将在服务器上运行,它是 PHP。在浏览器中,您需要使用相同模式的 JS 正则表达式。请更具体地说明doesnt work for me 的含义。
  • 是的,我需要使用 JS 正则表达式。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多