网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站建设 > 代码交流
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

代码交流 ASP,PHP,JSP等网站源代码下载与交流。

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1   IP: 222.76.215.18
Old 2008-01-24, 10:19 PM
car car is offline
高级会员
 
Join Date: 2006-05-14
Posts: 534
car 正向着好的方向发展
Default 邮政编码 验证

美国的邮政编码格式很简单,易于实现标准化和检验。其格式是5个数字,后面还可能有一个破折号及4个数字。虽然这种格式很简单,但判断输入的邮政编码是否实际存在还是有一定困难的,基本方法就是查看当前有效编码列表,而这些列表必须从美国邮政局才能得到。因此,程序清单11.2.1里的邮政编码处理库只实现了格式标准化及检验。

程序清单11.2.1 邮政编码函数库

<?php

// A function that will accept US zip codes

// and standardize them to xxxxx-xxxx format.

function standardize_zip($input) {

// First, remove all non-digits from the string

$digits = preg_replace('/[^0-9]/', '', $input);

// Grab the first five digits:

$ret = substr($digits, 0, 5);

// If there are more than 5, then include the rest after a dash

if (strlen($digits) > 5) {

$ret .= '-' . substr($digits, 5);

}

return $ret;

}

// A function to check for zip code validity

// It must have been standardized first.

function validate_zip($input) {

// First split the number into 2 parts:

$parts = explode('-', $input);

// If the first part is not 5 digits - Invalid

if (strlen($parts[0]) != 5) {

return false;

}

// If the second part exists, and is not 4 digits - Invalid

if (isset($parts[1]) && (strlen($parts[1]) != 4)) {

return false;

}

// Otherwise, we made it, it's valid.

return true;

}

// Standardize & validate some zips:

$zips = array('21771', '7177', 'x234 56', '12345-6789', '1313122',

'14142-77743', '21705-123', '2177-1234', '1-1', '7.42');

foreach ($zips as $num) {

$st = standardize_zip($num);

$valid = validate_zip($st);

$output = $valid ? 'Valid' : 'Invalid';

echo "<p>{$st} - {$output}</p>\n";

}

?>
Reply With Quote
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
google电话验证成功的关键 car Google Adsense 0 2007-12-16 05:44 PM
用session代替apache服务器验证 smiling 代码交流 0 2006-12-15 07:25 PM

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 11:08 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.