Posted By

Scooter on 04/25/09


Tagged


Versions (?)

Social Insurance Numbers


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2009/04/social-insurance-numbers.html

Functions for formatting and validating Social Insurance Numbers. Requires luhn().

  1. <?php
  2. /*
  3.   PHP Library - Social Insurance Number-related functions
  4.  
  5.   Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  6.  
  7.   This work is licensed under the Creative Commons Attribution License. To view
  8.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  9.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  10.   94305, USA.
  11.   */
  12.  
  13. // Format a Social Insurance Number.
  14. function formatSIN($socialInsuranceNumber)
  15. {
  16. if (eregi("^(\d{3})[\-\s]?(\d{3})[\-\s]?(\d{3})$", $socialInsuranceNumber))
  17. {
  18. return eregi_replace("^(\d{3})[\-\s]?(\d{3})[\-\s]?(\d{3})$", "$1 $2 $3", $socialInsuranceNumber);
  19. }
  20. else
  21. {
  22. die("Input cannot be formatted as a social insurance number.");
  23. }
  24. }
  25.  
  26. // Validate a Social Insurance Number.
  27. // REQUIRES: luhn()
  28. function isValidSIN($socialInsuranceNumber)
  29. {
  30. return (eregi("^([1-79]{3})[\-\s]?(\d{3})[\-\s]?(\d{3})$", $socialInsuranceNumber) && luhn($socialInsuranceNumber)) ? true : false;
  31. }
  32.  
  33. ?>

Report this snippet  

You need to login to post a comment.