How to validate SSN (Social Security Number) using Regular Expression
Last Updated :
26 Dec, 2022
Given string str, the task is to check whether the given string is valid SSN (Social Security Number) or not by using Regular Expression.
The valid SSN (Social Security Number) must satisfy the following conditions:
- It should have 9 digits.
- It should be divided into 3 parts by hyphen (-).
- The first part should have 3 digits and should not be 000, 666, or between 900 and 999.
- The second part should have 2 digits and it should be from 01 to 99.
- The third part should have 4 digits and it should be from 0001 to 9999.
Examples:
Input: str = “856-45-6789”;
Output: true
Explanation: The given string satisfies all the above mentioned conditions. Therefore, it is a valid SSN (Social Security Number).
Input: str = “000-45-6789”;
Output: false
Explanation: The given string starts with 000. Therefore, it is not a valid SSN (Social Security Number).
Input: str = “856-452-6789”;
Output: false
Explanation: The second part of this string has 3 digits. Therefore, it is not a valid SSN (Social Security Number).
Input: str = “856-45-0000”;
Output: false
Explanation: The third part of this string is 0000. Therefore, it is not a valid SSN (Social Security Number).
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
- Get the String.
- Create a regular expression to check valid SSN (Social Security Number) as mentioned below:
regex = "^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$";
- Where:
- ^ represents the starting of the string.
- (?!666|000|9\\d{2})\\d{3} represents the first 3 digits should not start with 000, 666, or between 900 and 999.
- – represents the string followed by a hyphen (-).
- (?!00)\\d{2} represents the next 2 digits should not start with 00 and it should be any from 01-99.
- – represents the string followed by a hyphen (-).
- (?!0{4})\\d{4} represents the next 4 digits can’t 0000 and it should be any from 0001-9999.
- $ represents the ending of the string.
- Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidSSN(string str)
{
const regex pattern( "^(?!666|000|9\\d{2})"
"\\d{3}-(?!00)"
"\\d{2}-(?!0{4})\\d{4}$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "856-45-6789" ;
cout << isValidSSN(str1) << endl;
string str2 = "000-45-6789" ;
cout << isValidSSN(str2) << endl;
string str3 = "856-452-6789" ;
cout << isValidSSN(str3) << endl;
string str4 = "856-45-0000" ;
cout << isValidSSN(str4) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidSSN(String str)
{
String regex = "^(?!666|000|9\\d{2})\\d{3}"
+ "-(?!00)\\d{2}-"
+ "(?!0{4})\\d{4}$" ;
Pattern p = Pattern.compile(regex);
if (str == null )
{
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "856-45-6789" ;
;
System.out.println(isValidSSN(str1));
String str2 = "000-45-6789" ;
;
System.out.println(isValidSSN(str2));
String str3 = "856-452-6789" ;
System.out.println(isValidSSN(str3));
String str4 = "856-45-0000" ;
System.out.println(isValidSSN(str4));
}
}
|
Python3
import re
def isValidSSN( str ):
regex = "^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "856-45-6789"
print (isValidSSN(str1))
str2 = "000-45-6789"
print (isValidSSN(str2))
str3 = "856-452-6789"
print (isValidSSN(str3))
str4 = "856-45-0000"
print (isValidSSN(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main( string [] args)
{
string [] str={ "856-45-6789" , "000-45-6789" , "856-452-6789" , "856-45-0000" };
foreach ( string s in str) {
Console.WriteLine( isValidSSN(s) ? "true" : "false" );
}
Console.ReadKey(); }
public static bool isValidSSN( string str)
{
string strRegex = @"^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isValidSSN(str) {
let regex = new RegExp(/^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "856-45-6789" ;
console.log(isValidSSN(str1));
let str2 = "000-45-6789" ;
console.log(isValidSSN(str2));
let str3 = "856-452-6789" ;
console.log(isValidSSN(str3));
let str4 = "856-45-0000" ;
console.log(isValidSSN(str4));
|
Output
true
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...