Introduction
Have ever had a field on a form for a users E-Mail address or phone number and the user put garbage values in?
I've written several programs that send E-Mails and Faxes using the information provided by user input. After spending 2 days once fixing blatant typos ie Someuser At Company dot Com, I decided to write a validation routine.
They don't stop all errors, but they do reduce user accidents.
BOOL ValidateEMail(CString Address)
{
BOOL RtnVal = TRUE;
if(Address.GetLength() < 5)
{
RtnVal = FALSE;
}
else if(Address.Find("@") == -1)
{
RtnVal = FALSE;
}
else if(Address.Find(".") == -1)
{
RtnVal = FALSE;
}
else if(Address.GetLength() - Address.ReverseFind('.') > 4)
{
RtnVal = FALSE;
}
else if(Address.ReverseFind('_') > Address.ReverseFind('@'))
{
RtnVal = FALSE;
}
else
{
int FindPos;
FindPos = Address.Find("@");
FindPos = Address.Find("@", FindPos+1);
if(FindPos != -1)
{
RtnVal = FALSE;
}
int Pos;
int NumChars;
NumChars = Address.GetLength();
Address.MakeUpper();
for(Pos = 0; Pos < NumChars; Pos ++)
{
if(
(!isdigit(Address[Pos]))
&& ((Address[Pos] < 'A') || (Address[Pos] > 'Z'))
&& (Address[Pos] != '_')
&& (Address[Pos] != '.')
&& (Address[Pos] != '@')
&& (Address[Pos] != '-'))
{
RtnVal = FALSE;
}
}
}
return RtnVal;
}
BOOL ValidatePhone(CString FaxNum)
{
BOOL RtnVal = TRUE;
if(FaxNum.GetLength() != 11)
{
RtnVal = FALSE;
}
else
{
int Pos;
int NumChars;
NumChars = FaxNum.GetLength();
FaxNum.MakeUpper();
for(Pos = 0; Pos < NumChars; Pos ++)
{
if(!isdigit(FaxNum[Pos]))
{
if(!isspace(FaxNum[Pos]))
RtnVal = FALSE;
}
}
}
return RtnVal;
}
Issues
The e-mail rules will need to change when the new domain names become available
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.