Advice on validating input
I need to validate input for name, address etc. This is how I'm gonna go about doing it.
1) name (broken down into first, middle, last) input has to be all letters.
2) address line1 and 2: has to be digits, letters, or space
I might want to elaborate that first portion is digits and the rest letters, space i.e 200 First Ave
3) city, state: letters, space
4) zip: 5 digits
The inputs are gonna be string type and will be read like this.
Code:
void Person::setFirstName(const string fn) {
int charValue;
int strLength = fn.size();
int count = 0;
bool invalidChar = false;
do
{
charValue = (int)fn[count]; // reads the ASCII equivalence
if ((charValue < 65 || bla...bla..bla.. { // not a letter
invalidChar = true;
}
count++;
} while (count < strLength && invalidChar != true);
if (invalidChar == false) {
firstName = fn;
}
else {
firstName = "XXXX";
}
}
What do you guys think? any suggestion welcomed