using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Text.RegularExpressions; namespace Dhaneel { public static class Validation { public static bool AllowOnlyAlphabetsAndSpace(string str) { char[] input = str.ToCharArray(); for (int i = 0; i < input.Length; i++) { if (!((Convert.ToInt32(input[i]) > 96 && Convert.ToInt32(input[i]) < 123) || (Convert.ToInt32(input[i]) > 64 && Convert.ToInt32(input[i]) < 91) || Convert.ToInt32(input[i]) == 32)) { return false; } } return true; } public static bool IsStringEmpty(string str) { if (String.IsNullOrEmpty(str)) { return true; } else { return false; } } public static bool IsOnlyAlphabets(string str) { Regex regex = new Regex("[^A-Z|^a-z|^ |^\t]"); if (regex.IsMatch(str)) { return false; } else { return true; } } public static bool IsNaturalNumbers(string str) { Regex regex = new Regex("0*[1-9][0-9]*"); if (regex.IsMatch(str)) { return true; } else { return false; } } public static bool IsAlphaNumeric(string str) { Regex regex = new Regex("[^a-zA-Z0-9]"); if (regex.IsMatch(str)) { return true; } else { return false; } } public static bool IsPositiveNumber(string str) { Regex objNotPositivePattern = new Regex("[^0-9.]"); Regex objPositivePattern = new Regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); return !objNotPositivePattern.IsMatch(str) && objPositivePattern.IsMatch(str) && !objTwoDotPattern.IsMatch(str); } public static bool IsNumber(string str) { Regex objNotNumberPattern = new Regex("[^0-9.-]"); Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*"); Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*"); String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$"; String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$"; Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")"); return !objNotNumberPattern.IsMatch(str) && !objTwoDotPattern.IsMatch(str) && !objTwoMinusPattern.IsMatch(str) && objNumberPattern.IsMatch(str); } public static bool IsValidEmail(string email) { //regular expression pattern for valid email //addresses, allows for the following domains: //com,edu,info,gov,int,mil,net,org,biz,name,museum,coop,aero,pro,tv 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})$"; //Regular expression object Regex check = new Regex(pattern, RegexOptions.IgnorePatternWhitespace); //boolean variable to return to calling method bool valid = false; //make sure an email address was provided if (string.IsNullOrEmpty(email)) { valid = false; } else { //use IsMatch to validate the address valid = check.IsMatch(email); } //return the value to the calling method return valid; } } }