Home:ALL Converter>11-character Social Security Number

11-character Social Security Number

Ask Time:2014-02-18T01:27:02         Author:user3320144

Json Formatter

I have written the code but i am stuck on for the Social security number(SSN) if-condition. I want SSN should be 11 characters with hyphen sign, if SSN not then display error message. Which convert class should I use for SSN. Here is my code:

double SSN;

Console.Write("Enter you Social Security Number: ");

SSN = Convert.ToDouble(Console.ReadLine());

// Social Security Number Check
if (SSN != 11)
{
    Console.WriteLine("Invalid Social Security Number.");
}

Author:user3320144,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21835508/11-character-social-security-number
ΩmegaMan :

Regular expressions are best used to determine if a number such as Social Security Number is valid or not; a validation of it so to speak. \n\nHere is an example which catches the user trying to add non digits, put in incorrect total number digits, as well as rejecting some obviously bad SSNs like 666 or 000. \n\nNote this is based on the current US Gov spec for numbers, see Social Security Number Randomization. \n\nvar pattern = @\" # To use this regex pattern specify IgnoreWhiteSpace due to these comments.\n ^ # Beginning of line anchor \n (?!9) # Can't be 900- 999 so stop match \n (?!000) # If it starts with 000 its bad (STOP MATCH!)\n (?!666) # If it starts with 666 its bad (STOP MATCH!)\n (?<FIRST>\\d{3}) # Match the First three digits and place into First named capture group\n (?:[\\s\\-]?) # Match but don't capture a possible space or dash\n (?<SECOND>\\d\\d) # Match next two digits\n (?:[\\s-]?) # Match but don't capture a possible space or dash\n (?<THIRD>\\d{4}) # Match the final for digits\n $ # EOL anchor\n\n \";\n\nConsole.WriteLine (Regex.IsMatch(\"123-45-6789\", pattern, RegexOptions.IgnorePatternWhitespace)); // True\nConsole.WriteLine (Regex.IsMatch(\"123-45-678A\", pattern, RegexOptions.IgnorePatternWhitespace)); // False\n\n\nThe option to IgnorePatternWhitespace only allows us to comment the pattern, it does not affect the regex parsing in anyway.\n\nThis answer is based on a blog post I have written and updated entitled Regular Expression Pattern for SSN using the Match Invalidator (?!) in .Net",
2014-02-17T17:48:15
CodeCaster :

With SSN != 11 you're not comparing the length of SSN with 11, but its value. As it is a numeric type, in this case a double, the user will have to literally enter 11 in order to pass the SSN test.\n\nYou can regard it as a string:\n\nstring ssn = Console.ReadLine();\n\n\nThen you could use a regex or LINQ to get only the numbers from the string, ignoring any other characters that may be entered:\n\nssn = new String(ssn.Where(x => Char.IsDigit(x)).ToArray());\n\n\nAfter which you can inspect ssn.Length to verify it has 9 digits. \n\nWhether those digits form a valid SSN, is another question.",
2014-02-17T17:44:59
yy