简体   繁体   English

PHP检查MySQL数据库中的邮政编码

[英]PHP check postal code in MySQL Database

I am trying to write a script to check someone's details they enter in a HTML form. 我正在尝试编写脚本来检查某人以HTML形式输入的详细信息。

I have the code fine but i have realised if they type the postcode in a different format to whats in the database its going to error. 我的代码很好,但我已经意识到,如果他们以与数据库中的内容不同的格式键入邮政编码,则会出错。

do i just take the POSTed postcode and change the format, if so whats the best way of making whats typed one format? 我是否只接受张贴的邮政编码并更改格式,如果是的话,使内容键入一种格式的最佳方法是什么?

for example, in the database there is a row with the postal code SS9 5LY but if someone types it as SS95LY or ss9 5ly it wont match 例如,在数据库中有一个邮政编码为SS9 5LY的行,但是如果有人将其键入为SS95LYss9 5lyss9 5ly不匹配

First, take a look to ZIP (POSTAL) Code Validation Regex . 首先,查看ZIP(邮政)代码验证正则表达式 Also, try to use a database records in your validation. 另外,请尝试在验证中使用数据库记录。

Have thousants of approachs to solve your problem. 有大量解决问题的方法。

You can use masked input , you can validate the input text before send to database using RegularExpression . 您可以使用屏蔽输入 ,可以在使用RegularExpression发送到数据库之前验证输入文本。 And many others. 还有许多其他。

You can use preg_replace() to remove any unwanted characters from the input, convert everything to upper case and finally add the space after the third character using substr() : 您可以使用preg_replace()从输入中删除所有不需要的字符,将所有内容转换为大写 ,最后使用substr()在第三个字符之后添加空格:

$postcode = preg_replace("/[^a-zA-Z0-9]+/", "", $postcode);
$postcode = strtoupper($postcode);
$postcode = substr($postcode, 0, 3) . ' ' . substr($postcode, 3);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM