//http://academicplanner.codeplex.com/
//Microsoft Public License (Ms-PL)
using System;
using System.Text.RegularExpressions;
namespace AcademicPlanner.Framework.Libraries.Validation
{
///
/// Provides static validation methods for string s.
///
/// 1.0
/// 1.0
public static class StringValidator
{
///
/// Regular expression for a valid email address: "^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$".
///
/// 1.0
/// 1.0
public static readonly string EMAIL_REGEXP = @"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$";
///
/// Regular expression for words with letters only: "^([a-zA-Z]+\s*)+$".
///
/// 1.0
/// 1.0
public static readonly string ALPHA_REGEXP = @"^([a-zA-Z]+\s*)+$";
///
/// Regurlar expression for words with letters and digits and "_" only: "^([a-zA-Z0-9_]+\s*)+$".
///
/// 1.0
/// 1.0
public static readonly string ALNUM_REGEXP = @"^([a-zA-Z0-9_]+\s*)+$";
///
/// Checks whether the given string is blank or not i.e.
/// it contains at least one character.
///
/// The string to check.
/// Whether the string is blank or not.
///
/// 1.0
/// 1.0
public static bool IsNotBlank(string pVal)
{
return ContainsAtLeast(pVal, 1);
}
///
/// Checks whether the given string is a valid email address or not i.e.
/// it matches the EMAIL_REGEXP regualar expression.
///
/// The string to check.
/// Whether the string is a valid email address or not.
///
///
/// 1.0
/// 1.0
public static bool IsEmailAddress(string pVal)
{
return Matches(pVal, EMAIL_REGEXP);
}
///
/// Checks whether the given string matches the given regualar expression.
///
/// The string to check.
/// The regular expression to check against.
/// Whether the string matches the given regualar expression.
/// 1.0
/// 1.0
public static bool Matches(string pVal, string pRegex)
{
return Regex.IsMatch(pVal, pRegex);
}
///
/// Checks whether the given string is words with letters only i.e.
/// it matches the ALPHA_REGEXP regualar expression.
///
/// The string to check.
/// Whether the string is words with letters only or not.
///
///
/// 1.0
/// 1.0
public static bool IsAlpha(string pVal)
{
return Matches(pVal, ALPHA_REGEXP);
}
///
/// Checks whether the given string is words with letters and digits only i.e.
/// it matches the ALNUM_REGEXP regualar expression.
///
/// The string to check.
/// Whether the string is words with letters and digits only or not.
///
///
/// 1.0
/// 1.0
public static bool IsAlphaNumeric(string pVal)
{
return Matches(pVal, ALNUM_REGEXP);
}
///
/// Checks whether the given string contains at least the the given
/// number of characters.
///
/// The string to check.
/// The minimum number of characters the string should have.
/// Whether the string contains at least the the given number of characters.
/// 1.0
/// 1.0
public static bool ContainsAtLeast(string pVal, int pMinChars)
{
return (pVal.Trim().Length >= pMinChars);
}
///
/// Checks whether the given string contains at most the the given
/// number of characters.
///
/// The string to check.
/// The maximum number of characters the string should have.
/// Whether the string contains at most the the given number of characters.
/// 1.0
/// 1.0
public static bool ContainsAtMost(string pVal, int pMaxChars)
{
return (pVal.Trim().Length <= pMaxChars);
}
///
/// Checks whether two string s are identical i.e. have the same characters
/// after trimming.
///
/// The first string .
/// The second string .
/// Whether the two string s are identical or not.
/// 1.0
/// 1.0
public static bool IsIdenticalTo(string pVal, string pComparee)
{
return pVal.Trim().Equals(pComparee.Trim());
}
}
}