网络营销电子商务研究中心  
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:20 PM
car car is offline
高级会员
 
Join Date: 2006-05-14
Posts: 534
car 正向着好的方向发展
Default php社会保障号(SSN)验证

社会保障号(SSN)
在美国,每个人都被分配一个“社会保障号(SSN)”,它在很多场合都成为核查身份的手段,因此经常是表单上的必填字段。它总是由3位数字开始,接是2位数字,接着又是4位数字。在显示这个号码时,三个部分之间通常以破折号分隔,但有时人们会忘记。

在检验时还可以使用更多的规则。这三个部分之中任意一个部分都不能全是0。目前,前三位数字(区号)不能比772大,因为那些数字还没有被分配。当然,在新的号码被分配之后,脚本就要做相应的修改。程序清单11.3.1展示了SSN标准化及检验例程。

程序清单11.3.1 社会保障号函数库

<?php

// A function that will accept US SSNs

// and standardize them to xxx-xx-xxxx format.

function standardize_ssn($ssn) {

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

$s = preg_replace('/[^0-9]/', '', $ssn);

// Now, break it into it's appropriate parts and insert dashes.

return substr($s, 0, 3) . '-' . substr($s, 3, 2) . '-' . substr($s, 5);

}

// A function to check for SSN validity, requires a standardized number

function validate_ssn($ssn) {

// First split the number into 3 parts:

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

// If any part is all 0's - Invalid

foreach ($parts as $p) {

if ($p == 0) {

return false;

}

}

// If the first part is greater than 772 (May need updated in future)

if ($parts[0] > 772) {

return false;

}

// Finally, if the final part is not 4 characters, it is invalid

if (strlen($parts[2]) != 4) {

return false;

}

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

return true;

}

// Standardize & validate some SSN:

$ssn = array('774 35 4688', '354-00-0103', '123456789', 'Hello');

foreach ($ssn as $num) {

$st = standardize_ssn($num);

$valid = validate_ssn($st);

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

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

}

?>

如果需要,可以通过访问美国社会保障管理局站点(http://ssa.gov/)来进一步严格地检查SSN的有效性,那里有...SN查询表。
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

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


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