Jump to content

Help with SSN validation


PHPandME1

Recommended Posts

I am very much a php beginner and I am trying to figure out how to write some to code to validate a social security number. The question I am working on is:

 

"Write a PHP function called isValidSSN that validates a social security number. The function takes 2 parameters. The first parameter is a string that represents the ssn. The second parameter is a reference parameter that will store the error message generated by the function when the ssn is incorrect. The error message should indicate if a ssn was not provided or if the ssn that was provided is in the wrong format. A valid ssn must be in the format ###-##-#### where # is a digit and the dashes MUST be included. The function should return true if the ssn is valid and false if the ssn is invalid."

 

I am completely lost so any assistance would be much appreciated. Thank you!

 

 

Link to comment
https://forums.phpfreaks.com/topic/278144-help-with-ssn-validation/
Share on other sites

There are various ways to accomplish the validation. You can use trim and a comparison to the empty string to check if it was not provided. You could learn some basic regular expressions and use preg_match to match the required format. Another way to match the format would be a combination of explode, ctype_digit and strlen.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.