This handy method using Java validates if a U.S. social security number represented as a String is valid or not. Some format examples can either be 879-89-8989 or 869878789 and many others. This method uses a REGEX expression for validation with the following details:

  • ^\\d{3}: Starts with three numeric digits.
  • [- ]?: Followed by an optional “-“
  • \\d{2}: Two numeric digits after the optional “-“
  • [- ]?: May contain an optional second “-” character.
  • \\d{4}: ends with four numeric digits.

Check the code below.

Related Posts Plugin for WordPress, Blogger...