how can i make a validation for this code for the name? it has to be at least on
ID: 3724361 • Letter: H
Question
how can i make a validation for this code for the name? it has to be at least one character long, after being trimmed. It has to test for a valid value. if it is invalid then set the area code attribute to string . Empty
public class Telephone
{
public string sName
{
get
{
return sName;
}
set
{
sName = value;
}
}
public int iAreaCode
{
get
{
return iAreaCode;
}
set
{
iAreaCode = value;
}
}
public int iPhoneNumber
{
get
{
return iPhoneNumber;
}
set
{
iPhoneNumber = value;
}
}
public bool Valid
{
get
{
return this.Valid;
}
set
{
this.Valid = value;
}
}
static void Main(string[] args)
{
Telephone person = new Telephone();
string sName;
int iAreaCode;
int iPhoneNumber;
bool AreaCodevalid;
bool PhoneNumber;
Console.Write("Please Enter Person's Name: ");
sName = Console.ReadLine().Trim();
Console.Write("Please enter the area code: ");
int.TryParse(Console.ReadLine().Trim(), out iAreaCode);
if (((iAreaCode >= 100) && (iAreaCode <= 799) || (iAreaCode >= 900) && (iAreaCode <= 999)))
{
AreaCodevalid = true;
}
else
{
AreaCodevalid = false;
}
Console.WriteLine("INVALID - must be 100-799 or 900 - 999 {0}", AreaCodevalid);
Console.Write("Please enter the number: ");
int.TryParse(Console.ReadLine().Trim(), out iPhoneNumber);
Console.WriteLine();
if (((iPhoneNumber >= 1000000) && (iPhoneNumber <= 9999999)))
{
PhoneNumber = true;
}
else
{
PhoneNumber = false;
}
Console.WriteLine("INVALID - must have 7 digits {0}", PhoneNumber);
Console.WriteLine(" ");
printContact(sName, iAreaCode, iPhoneNumber);
}
static void printContact(string sName, int iAreaCode, int iPhoneNumber)
{
Console.WriteLine("Contact Information:");
Console.WriteLine("====================");
Console.WriteLine("Name: {0} Telephone number: {1}-{2} ", sName, iAreaCode, iPhoneNumber); // ** Complete this line using properties
Console.Write("Press any key to exit...");
Console.ReadKey();
}
}
}
Explanation / Answer
Hi,
You can use below code:
Console.Write("Please Enter Person's Name: ");
sName = Console.ReadLine().Trim();
if (sName.length() < 1)
{
//sName = Integer.toString(iAreaCode) or whatever you want to assign, your comment about //assignment is not very much clear in the question
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.