Validation of an email address.
★★★A valid email address consists of an email prefix and an email domain, both in acceptable formats. The prefix appears to the left of the @ symbol. The domain appears to the right of the @ symbol. For example, in the address [email protected], "example" is the email prefix, and "mail.com" is the email domain. Basic validation of an address is carried out client-side before data is sent to be processed server-side.
Write a program that validates an email address.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
Acceptable email prefix formats:
Acceptable email domain formats:
validate that:email_address is valid or false if not.Enter your email address:
[email protected]
Email address is invalid.
Enter your email address:
[email protected]
Email address is valid.
Enter your email address:
[email protected]
Email address is invalid.
Enter your email address:
[email protected]
Email address is valid.
Enter your email address:
[email protected]@
Email address is invalid.
Enter your email address:
[email protected]
Email address is valid.
// Valid address program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
static bool validate(string email_address)
{
---
// Validate prefix
bool valid = true;
int index = 0;
char last_character = ' ';
int at_position = email_address.IndexOf("@");
---
// Invalid if no @ symbol
if (at_position == -1)
{
return false;
}
---
// Check each character is valid and stop once address is invalid
while (valid && index < at_position)
{
---
// Digits are acceptable
if (char.IsDigit(email_address[index]))
{
valid = true;
}
---
// Alphabet characters are acceptable
else if (char.IsLetter(email_address[index]))
{
valid = true;
}
---
// Underscore, period or hyphen is acceptable but not two in a row
else if ("_.-".Contains(email_address[index]) && !"_.-".Contains(last_character))
{
valid = true;
}
---
// If conditions are not met it is an invalid email address
else
{
return false;
}
---
last_character = email_address[index];
index++;
}
---
// A separator may not be the last character before the @ symbol
if ("_.-".Contains(last_character))
{
return false;
}
---
// Validate domain
// Domain must be at least three characters
if (email_address.Length - at_position - 1 < 3)
{
return false;
}
index = at_position + 1;
---
// Check domain
while (valid && index < email_address.Length)
{
---
// Digits are acceptable
if (char.IsDigit(email_address[index]))
{
valid = true;
}
---
// Alphabet characters are acceptable
else if (char.IsLetter(email_address[index]))
{
valid = true;
}
---
// Hyphens and periods are acceptable
else if ("-.".Contains(email_address[index]))
{
valid = true;
}
---
else
{
return false;
}
---
index++;
}
return valid;
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Enter your email address: ");
string email_address = Console.ReadLine();
bool valid = validate(email_address);
---
if (valid)
{
Console.WriteLine("Email address is valid.");
}
else
{
Console.WriteLine("Email address is invalid.");
}
---
}
}