C# simple email and phone validation using Regular Expressions

	string emailRegex = @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|ca|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b";
	string phoneRegex = @"^\d{10}$";

	Match phoneMatch = Regex.Match(phone, phoneRegex);
	Match emailMatch = Regex.Match(email, emailRegex);
	
	if (!emailMatch.Success)
	{
		yield return new ValidationResult("Email is invalid", new[] { "ContactEmail" });
	}

Leave a Reply

Your email address will not be published. Required fields are marked *