using System;
using System.Collections.Generic;
using System.Linq; /*** dotNet version ***/
using System.Text;
using System.Text.RegularExpressions;
/*
* @Name: ValidatorLib
* @Description: String validation
* @Author: Damjan Krstevski - SkyDriver
* @Date: 20.11.2010
* @License: Freeware, you can use, edit, copy, redistribute...
* @FeedBack: krstevsky[at]gmail[dot]com
*/
namespace ValidatorLib
{
/// <summary>
/// Class for validation of the string
/// </summary>
class Validator
{
#region Class variable(s)
/// <summary>
/// Class value
/// </summary>
private string strText = string.Empty;
#endregion
#region Constructor(s)
/// <summary>
/// Default constructor
/// </summary>
public Validator()
{
}
/// <summary>
/// Other constructor with text value
/// </summary>
/// <param name="text">String, the string for validation</param>
public Validator(string text)
{
this.strText = text;
}
#endregion
#region SET and GET methods
/// <summary>
/// Set or Get the string value
/// </summary>
public string text
{
get { return this.strText; }
set { this.strText = value; }
}
#endregion
#region Function: isNullOrEmpty
/// <summary>
/// Check if the string is null or empty
/// </summary>
/// <returns>Bool, true if the string is null or empty</returns>
public bool isNullOrEmpty()
{
return string.IsNullOrEmpty(this.strText);
}
/// <summary>
/// Check if the string is null or empty
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <returns>Bool, true if the string is null or empty</returns>
public bool isNullOrEmpty(string str)
{
return string.IsNullOrEmpty(str);
}
#endregion
#region Function: Length validation
/// <summary>
/// Check string length (maximum length)
/// </summary>
/// <param name="maxLength">Int, the maximum allowed length</param>
/// <returns>Bool, true if the string length is smallest or equal with the maximum allowed length</returns>
public bool isLengthMax(int maxLength)
{
return this.strText.Length <= maxLength ? true : false;
}
/// <summary>
/// Check string length (minimum length)
/// </summary>
/// <param name="minLength">Int, the minimum allowed length</param>
/// <returns>Bool, true if the string length is biggest or equal with the minimum allowed length</returns>
public bool isLengthMin(int minLength)
{
return this.strText.Length >= minLength ? true : false;
}
/// <summary>
/// Check string length according given length range (minimum & maximum)
/// </summary>
/// <param name="min">Int, minimum allowed length</param>
/// <param name="max">Int, maximum allowed length</param>
/// <returns>Bool, true if the string length is between the given length range</returns>
public bool isLength(int min, int max)
{
return this.strText.Length >= min && this.strText.Length <= max ? true : false;
}
/// <summary>
/// Check the string length (maximum length)
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <param name="maxLength">Int, maximum allowed length</param>
/// <returns>Bool, true if the string length is smallest or equal with the allowed length</returns>
public bool isLengthMax(string str, int maxLength)
{
return str.Length <= maxLength ? true : false;
}
/// <summary>
/// Check the string length (minimum length)
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <param name="minLength">Int, the minimum allowed length</param>
/// <returns>Bool, true if the string length is biggest or equal with the allowed length</returns>
public bool isLengthMin(string str, int minLength)
{
return str.Length >= minLength ? true : false;
}
/// <summary>
/// Check the string length according given length range (minimum & maximum)
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <param name="min">Int, minimum allowed length</param>
/// <param name="max">Int, maximum allowed length</param>
/// <returns>Bool, true if the string length is between given length range</returns>
public bool isLength(string str, int min, int max)
{
return str.Length >= min && str.Length <= max ? true : false;
}
#endregion
#region Functions: is number, alpha, alpha-numeric...
/// <summary>
/// Check the string if is a number
/// </summary>
/// <returns>Bool, true if the string is a number</returns>
public bool isNumber()
{
int tmp_n = 0;
return int.TryParse(this.strText, out tmp_n);
}
/// <summary>
/// Check if the string contains only alpha character(s)
/// </summary>
/// <returns>Bool, true if the string contains only alpha characters</returns>
public bool isAlpha()
{
Regex r = new Regex(@"[^a-zA-Z]");
return !r.IsMatch(this.strText);
}
/// <summary>
/// Check if the string contains only alpha character(s)
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <returns>Bool, true if the string contains only alpha characters</returns>
public bool isAlpha(string str)
{
Regex r = new Regex(@"[^a-zA-Z]");
return !r.IsMatch(str);
}
/// <summary>
/// Check if the string contains only alpha-numeric characters
/// </summary>
/// <returns>Bool, true if the string contains only alpha-numeric characters</returns>
public bool isAlphaNumeric()
{
Regex r = new Regex(@"[^a-zA-Z0-9]");
return !r.IsMatch(this.strText);
}
/// <summary>
/// Check if the string contains only alpha-numeric characters
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <returns>Bool, if the string contains only alpha-numeric characters</returns>
public bool isAlphaNumeric(string str)
{
Regex r = new Regex(@"[^a-zA-Z0-9]");
return !r.IsMatch(str);
}
#endregion
#region Function: Mail validation
/// <summary>
/// Check if the mail is valid
/// </summary>
/// <returns>Bool, true if the mail address is valid</returns>
public bool isMailValid()
{
string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
Regex r = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
return r.IsMatch(this.strText);
}
/// <summary>
/// Check if the mail is valid
/// </summary>
/// <param name="mail">String, the mail address for validation</param>
/// <returns>Bool, true if the mail address is valid</returns>
public bool isMailValid(string mail)
{
string pattern = @"^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.
(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$";
Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
return check.IsMatch(mail);
}
#endregion
#region Functions: is lower, upper...
/// <summary>
/// Check if the string contains only lower characters
/// </summary>
/// <returns>Bool, true if the string contains only lower charaters</returns>
public bool isLower()
{
Regex r = new Regex(@"[^a-z]$");
return !r.IsMatch(this.strText);
}
/// <summary>
/// Check if the string contains only lower characters
/// </summary>
/// <param name="str">String, string for validation</param>
/// <returns>Bool, true if the string contains only lower charaters</returns>
public bool isLower(string str)
{
Regex r = new Regex(@"[^a-z]$");
return !r.IsMatch(str);
}
/// <summary>
/// Check if the string contains only upper characters
/// </summary>
/// <returns>Bool, true if the string contains only upper characters</returns>
public bool isUpper()
{
Regex r = new Regex(@"[^A-Z]$");
return !r.IsMatch(this.strText);
}
/// <summary>
/// Check if the string contains only upper characters
/// </summary>
/// <param name="str">String, the string for validation</param>
/// <returns>Bool, true if the string contains only upper characters</returns>
public bool isUpper(string str)
{
Regex r = new Regex(@"[^A-Z]$");
return !r.IsMatch(str);
}
#endregion
}
}