SSN and Phone Num formatting

Aug 10, 2012 at 10:39pm
Can someone explain how to validate format of user input for SSN and Phone Number?
SSN format should be ###-##-####
and phone number should be ###-###-####

So far I can check the number of digits, and that they are all digits, but i don't know how to check for the above format...I can't find any examples on my text, and I dont think I understand string functions as well as I could, so any help would be appreciated!
Aug 10, 2012 at 10:59pm
closed account (S6k9GNh0)
Well, I coded an example for you for about 5 minutes and cplusplus.com decided I was no longer logged in and I have to redo it all which I'm not going to do, sorry.

Basically the gist of it all is use a parser/tokenizer such as bison/flex or boost.spirit, or spend a lot more time writing it out by hand for almost no benefit at this point.

You can do this one by hand since it won't take long though. Basically, just check for the size of the string to make sure it's the correct size, then iterate through each character in the string to make sure it's the character its supposed to be. Its not hard at all, try not to over-complicate things. I would look into the concept of a grammar "token". You can use std::string.find() to see if a character is inside of a token rule.
Last edited on Aug 10, 2012 at 11:02pm
Aug 10, 2012 at 11:04pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
string ReadAndValidateUserSocial (string userSocial)
{
	bool flag = true;

	while (flag)
	{
		cout << "Enter Social Security Number: ";
		cin >> userSocial;

		int userSSN = atoi (userSocial.c_str ());

		if (SSN_LENGTH > userSSN > SSN_LENGTH)
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl << endl;
		}
		else
		{
			flag = false;
		}

		if (!isdigit (userSSN))
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl << endl;
		}
		else
		{
			flag = false;
		}
	}
	
	return userSocial;
}


So far I have this...I am still confused though on how to check for format..
Aug 10, 2012 at 11:05pm
So I should keep it as a string? And try to validate each character within the string? Rather than converting to int?
Aug 10, 2012 at 11:25pm
closed account (S6k9GNh0)
I'm not sure how atoi is supposed to handle dashes... probably not very conforming either.

I suppose you could just remove the dashes in the string but this assumes they're in the correct location and that the rest of the string has valid numeric characters. I'm not sure the best way to go about this. I would think parsing the string as is would be the safest bet but probably not the fastest one.
Last edited on Aug 10, 2012 at 11:26pm
Aug 10, 2012 at 11:54pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
string ReadAndValidateUserSocial (string userSocial)
{
	bool check = false;

	while (!check)
	{
		cout << "Enter Social Security Number (###-##-####): ";
		cin >> userSocial;

		if (userSocial.length () == SSN_LENGTH)
		{
			if (userSocial [3] == '-' && userSocial [6] == '-')
			{
				userSocial = userSocial.replace (3, 1, "");
				userSocial = userSocial.replace (6, 1, "");

				for (int i = 0; i < SSN_LENGTH; i++)
				{
					if (isdigit (userSocial [i]))
					{
						check = true;
					}
				}
			}
		}

		if (check == false)
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl;
		}
	}
	
	return userSocial;
}


What about this? Would this work?
Aug 11, 2012 at 12:32am
Ok, so I have gotten it EXCEPT for...isdigit. It is not checking every instance, i can still enter abc-12-1234 and it allows me to. I need them to ALL be numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

string ReadAndValidateUserSocial (string userSocial);

int main ()
{
	string userSocial;

	ReadAndValidateUserSocial (userSocial);

	system ("PAUSE");
	return 0;
}

string ReadAndValidateUserSocial (string userSocial)
{
	bool check = false;

	while (!check)
	{
		cout << "Enter Social Security Number (###-##-####): ";
		cin >> userSocial;

		if (userSocial.length () == SSN_LENGTH)
		{
			if (userSocial [3] == '-' && userSocial [6] == '-')
			{
				userSocial = userSocial.replace (3, 1, "");
				userSocial = userSocial.replace (6, 1, "");

				for (int i = 0; i < userSocial.length(); i++)
				{
					if (isdigit (userSocial [i]))
					{
						check = true;
					}
				}
			}
		}

		if (check == false)
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl;
		}
	}
	
	return userSocial;
}
Aug 11, 2012 at 12:59am
You have an issue with you second .replace. Remember, after the first replace, the string is resized, you actually want userSocial.replace(5,1,"");

Also, your code is backwards. You want to check if the person entered any non numeric values. Here is your code rewritten:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
string ReadAndValidateUserSocial(string userSocial) {
   bool check = false;

   while (!check) {
      check = true;
      cout << "Enter Social Security Number (###-##-####): ";
      cin >> userSocial;

      if (userSocial.length() == SSN_LENGTH) {
         if (userSocial [3] == '-' && userSocial [6] == '-') {
            userSocial = userSocial.replace(3, 1, "");
            userSocial = userSocial.replace(5, 1, "");

            for (int i = 0; i < userSocial.length(); i++) {
               if (!isdigit(userSocial [i])) {
                  check = false;
               }
            }
         }
      }

      if (!check) {
         cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl;
      }
   }

   return userSocial;
}


I underlined the parts that I changed.
Last edited on Aug 11, 2012 at 12:59am
Aug 11, 2012 at 1:03am
I understand the changes you made, i guess now my issue is...
i can enter 123-1-1234 and it lets it work...which I didn't think it did before.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

string ReadAndValidateUserSocial (string userSocial);

int main ()
{
	string userSocial;

	ReadAndValidateUserSocial (userSocial);

	system ("PAUSE");
	return 0;
}

string ReadAndValidateUserSocial (string userSocial)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Social Security Number (###-##-####): ";
		cin >> userSocial;

		if (userSocial.length () == SSN_LENGTH)
		{
			if (userSocial [3] == '-' && userSocial [6] == '-')
			{
				userSocial = userSocial.replace (3, 1, "");
				userSocial = userSocial.replace (5, 1, "");

				for (int i = 0; i < userSocial.length(); i++)
				{
					if (!isdigit (userSocial [i]))
					{
						check = false;
					}
				}
			}
		}

		if (!check)
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl;
		}
	}
	
	return userSocial;
}
Aug 11, 2012 at 1:19am
1
2
3
4
5
6
7
8
// Add this
		}
		else
			check = false;
		if (!check)
		{
			cout << "Invalid Entry! Social Security Number must be 9 digits! Please try again." << endl;
		}
Last edited on Aug 11, 2012 at 1:20am
Aug 11, 2012 at 1:20am
Before I add that, can you look this over and let me know if you see any errors in this? I redid a few things and found that this one works the best so far...i have tried almost every entry possible and i am getting the correct outcome...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

const int SSN_LENGTH = 11;

string ReadAndValidateUserSocial (string userSocial);

int main ()
{
	string userSocial;

	ReadAndValidateUserSocial (userSocial);

	system ("PAUSE");
	return 0;
}

string ReadAndValidateUserSocial (string userSocial)
{
	bool check = false;

	while (!check)
	{
		check = true;

		cout << "Enter Social Security Number (###-##-####): ";
		cin >> userSocial;

		if (userSocial.length () != SSN_LENGTH)
		{
			check = false;
		}
		
		if (userSocial [3] != '-' && userSocial [6] != '-')
		{
			check = false;
		}
		else
		{
			userSocial = userSocial.replace (3, 1, "");
			userSocial = userSocial.replace (5, 1, "");

			for (int i = 0; i < userSocial.length(); i++)
			{
				if (!isdigit (userSocial [i]))
				{
					check = false;
				}
			}
		}
		
		if (!check)
		{
			cout << "Invalid Entry! Please try again." << endl;
		}
	}
	
	return userSocial;
}
Topic archived. No new replies allowed.