Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. @"^([w.-]+)@([w-]+)((.(w){2,3})+)$"
  2.  
  3. public bool IsValid(string emailaddress)
  4. {
  5. try
  6. {
  7. MailAddress m = new MailAddress(emailaddress);
  8.  
  9. return true;
  10. }
  11. catch (FormatException)
  12. {
  13. return false;
  14. }
  15. }
  16.  
  17. string email = txtemail.Text;
  18. Regex regex = new Regex(@"^([w.-]+)@([w-]+)((.(w){2,3})+)$");
  19. Match match = regex.Match(email);
  20. if (match.Success)
  21. Response.Write(email + " is correct");
  22. else
  23. Response.Write(email + " is incorrect");
  24.  
  25. @"^[w!#$%&'*+-/=?^_`{|}~]+(.[w!#$%&'*+-/=?^_`{|}~]+)*"
  26. + "@"
  27. + @"((([-w]+.)+[a-zA-Z]{2,4})|(([0-9]{1,3}.){3}[0-9]{1,3}))$";
  28.  
  29. bool IsValidEmail(string strIn)
  30. {
  31. // Return true if strIn is in valid e-mail format.
  32. return Regex.IsMatch(strIn, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
  33. }
  34.  
  35. Invalid: @majjf.com
  36. Invalid: A@b@[email protected]
  37. Invalid: Abc.example.com
  38. Invalid: js*@proseware.com
  39. Invalid: ma@@jjf.com
  40. Invalid: ma@jjf.
  41. Invalid: ma_@jjf
  42. Invalid: ma_@jjf.
  43. Invalid: -------
  44. Valid: j_9@[129.126.118.1]
  45. Invalid: js#[email protected]
  46.  
  47. @"^([0-9a-zA-Z]([+-_.][0-9a-zA-Z]+)*)+"@(([0-9a-zA-Z][-w]*[0-9a-zA-Z]*.)+[a-zA-Z0-9]{2,17})$";
  48.  
  49. const String pattern =
  50. @"^([0-9a-zA-Z]" + //Start with a digit or alphabetical
  51. @"([+-_.][0-9a-zA-Z]+)*" + // No continuous or ending +-_. chars in email
  52. @")+" +
  53. @"@(([0-9a-zA-Z][-w]*[0-9a-zA-Z]*.)+[a-zA-Z0-9]{2,17})$";
  54.  
  55. var validEmails = new[] {
  56. };
  57. var invalidEmails = new[] {
  58. "Abc.example.com", // No `@`
  59. "A@b@[email protected]", // multiple `@`
  60. "[email protected]", // continuous multiple dots in name
  61. "[email protected]", // only 1 char in extension
  62. "[email protected]", // continuous multiple dots in domain
  63. "ma@@jjf.com", // continuous multiple `@`
  64. "@majjf.com", // nothing before `@`
  65. "[email protected]", // nothing after `.`
  66. "[email protected]", // nothing after `_`
  67. "ma_@jjf", // no domain extension
  68. "ma_@jjf.", // nothing after `_` and .
  69. "ma@jjf.", // nothing after `.`
  70. };
  71.  
  72. foreach (var str in validEmails)
  73. {
  74. Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
  75. }
  76. foreach (var str in invalidEmails)
  77. {
  78. Console.WriteLine("{0} - {1} ", str, Regex.IsMatch(str, pattern));
  79. }
  80.  
  81. name@hotmail
  82.  
  83.  
  84. using System.Text.RegularExpressions;
  85.  
  86. public static bool IsValidEmail(string email)
  87. {
  88. return Regex.IsMatch(email, @"A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*.)+[a-z]{2,4}z")
  89. && Regex.IsMatch(email, @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*");
  90. }
  91.  
  92. public bool IsValidEmailAddress(string s)
  93. {
  94. if (string.IsNullOrEmpty(s))
  95. return false;
  96. else
  97. {
  98. var regex = new Regex(@"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*");
  99. return regex.IsMatch(s) && !s.EndsWith(".");
  100. }
  101. }
  102.  
  103. public static bool IsValidEmailAddress(this string s)
  104. {
  105. var regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
  106. return regex.IsMatch(s);
  107. }
  108.  
  109. myEmailString.IsValidEmailAddress();
  110.  
  111. var myPattern = Regex.EmailPattern;
  112.  
  113. /// <summary>
  114. /// Validates the string is an Email Address...
  115. /// </summary>
  116. /// <param name="emailAddress"></param>
  117. /// <returns>bool</returns>
  118. public static bool IsValidEmailAddress(this string emailAddress)
  119. {
  120. var valid = true;
  121. var isnotblank = false;
  122.  
  123. var email = emailAddress.Trim();
  124. if (email.Length > 0)
  125. {
  126. // Email Address Cannot start with period.
  127. // Name portion must be at least one character
  128. // In the Name, valid characters are: a-z 0-9 ! # _ % & ' " = ` { } ~ - + * ? ^ | / $
  129. // Cannot have period immediately before @ sign.
  130. // Cannot have two @ symbols
  131. // In the domain, valid characters are: a-z 0-9 - .
  132. // Domain cannot start with a period or dash
  133. // Domain name must be 2 characters.. not more than 256 characters
  134. // Domain cannot end with a period or dash.
  135. // Domain must contain a period
  136. isnotblank = true;
  137. valid = Regex.IsMatch(email, Regex.EmailPattern, RegexOptions.IgnoreCase) &&
  138. !email.StartsWith("-") &&
  139. !email.StartsWith(".") &&
  140. !email.EndsWith(".") &&
  141. !email.Contains("..") &&
  142. !email.Contains(".@") &&
  143. !email.Contains("@.");
  144. }
  145.  
  146. return (valid && isnotblank);
  147. }
  148.  
  149. /// <summary>
  150. /// Validates the string is an Email Address or a delimited string of email addresses...
  151. /// </summary>
  152. /// <param name="emailAddress"></param>
  153. /// <returns>bool</returns>
  154. public static bool IsValidEmailAddressDelimitedList(this string emailAddress, char delimiter = ';')
  155. {
  156. var valid = true;
  157. var isnotblank = false;
  158.  
  159. string[] emails = emailAddress.Split(delimiter);
  160.  
  161. foreach (string e in emails)
  162. {
  163. var email = e.Trim();
  164. if (email.Length > 0 && valid) // if valid == false, no reason to continue checking
  165. {
  166. isnotblank = true;
  167. if (!email.IsValidEmailAddress())
  168. {
  169. valid = false;
  170. }
  171. }
  172. }
  173. return (valid && isnotblank);
  174. }
  175.  
  176. public class Regex
  177. {
  178. /// <summary>
  179. /// Set of Unicode Characters currently supported in the application for email, etc.
  180. /// </summary>
  181. public static readonly string UnicodeCharacters = "À-ÿp{L}p{M}ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü«»€₣äÄöÖüÜß"; // German and French
  182.  
  183. /// <summary>
  184. /// Set of Symbol Characters currently supported in the application for email, etc.
  185. /// Needed if a client side validator is being used.
  186. /// Not needed if validation is done server side.
  187. /// The difference is due to subtle differences in Regex engines.
  188. /// </summary>
  189. public static readonly string SymbolCharacters = @"!#%&'""=`{}~.-+*?^|/$";
  190.  
  191. /// <summary>
  192. /// Regular Expression string pattern used to match an email address.
  193. /// The following characters will be supported anywhere in the email address:
  194. /// ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü«»€₣äÄöÖüÜß[a - z][A - Z][0 - 9] _
  195. /// The following symbols will be supported in the first part of the email address(before the @ symbol):
  196. /// !#%&'"=`{}~.-+*?^|/$
  197. /// Emails cannot start or end with periods,dashes or @.
  198. /// Emails cannot have two @ symbols.
  199. /// Emails must have an @ symbol followed later by a period.
  200. /// Emails cannot have a period before or after the @ symbol.
  201. /// </summary>
  202. public static readonly string EmailPattern = String.Format(
  203. @"^([w{0}{2}])+@{1}[w{0}]+([-.][w{0}]+)*.[w{0}]+([-.][w{0}]+)*$", // @"^[{0}w]+([-+.'][{0}w]+)*@[{0}w]+([-.][{0}w]+)*.[{0}w]+([-.][{0}w]+)*$",
  204. UnicodeCharacters,
  205. "{1}",
  206. SymbolCharacters
  207. );
  208. }
  209.  
  210. w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*
  211.  
  212. public bool IsEmail(string text)
  213. {
  214. Regex regex = new Regex(@"^(([w-]+.)+[w-]+|([a-zA-Z]{1}|[w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9]).([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9]).([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z]+[w-]+.)+[a-zA-Z]{2,4})$");
  215. return regex.IsMatch(text);
  216. }
  217.  
  218. public bool VailidateEntriesForAccount()
  219. {
  220. if (!(txtMailId.Text.Trim() == string.Empty))
  221. {
  222. if (!IsEmail(txtMailId.Text))
  223. {
  224. Logger.Debug("Entered invalid Email ID's");
  225. MessageBox.Show("Please enter valid Email Id's" );
  226. txtMailId.Focus();
  227. return false;
  228. }
  229. }
  230. }
  231. private bool IsEmail(string strEmail)
  232. {
  233. Regex validateEmail = new Regex("^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z] {2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$");
  234. return validateEmail.IsMatch(strEmail);
  235. }
  236.  
  237. if (!System.Text.RegularExpressions.Regex.IsMatch("<Email String Here>", @"^([w.-]+)@([w-]+)((.(w){2,3})+)$"))
  238. {
  239. MessageBox.show("Incorrect Email Id.");
  240. }
  241.  
  242. public class LoginViewModel
  243. {
  244. [EmailAddress(ErrorMessage = "The email format is not valid")]
  245. public string Email{ get; set; }
  246.  
  247. public static bool ValidateEmail(string str)
  248. {
  249. return Regex.IsMatch(str, @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*");
  250. }
  251.  
  252. using System.Text.RegularExpressions;
  253. if (!Regex.IsMatch(txtEmail.Text, @"^[a-z,A-Z]{1,10}((-|.)w+)*@w+.w{3}$"))
  254. MessageBox.Show("Not valid email.");
  255.  
  256. string EmailPattern = @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*";
  257. if (Regex.IsMatch(Email, EmailPattern, RegexOptions.IgnoreCase))
  258. {
  259. Console.WriteLine("Email: {0} is valid.", Email);
  260. }
  261. else
  262. {
  263. Console.WriteLine("Email: {0} is not valid.", Email);
  264. }
  265.  
  266. string patternEmail = @"(?<email>w+@w+.[a-z]{0,3})";
  267. Regex regexEmail = new Regex(patternEmail);
  268.  
  269. ^[w!#$%&'*+-/=?^_`{|}~]+(.[w!#$%&'*+-/=?^_`{|}~]+)*@((([-w]+.)+[a-zA-Z]{2,4})|(([0-9]{1,3}.){3}[0-9]{1,3}))$
  270.  
  271. ^(([^<>()[]\.,;:s@""]+(.[^<>()[]\.,;:s@""]+)*)|("".+""))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$
  272.  
  273. public static bool IsValidEmail(string email)
  274. {
  275. var r = new Regex(@"^([0-9a-zA-Z]([-.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+[a-zA-Z]{2,9})$");
  276. return !String.IsNullOrEmpty(email) && r.IsMatch(email);
  277. }
  278.  
  279. public static bool IsValidEmailAddress(this string emailaddress)
  280. {
  281. try
  282. {
  283. MailAddress m = new MailAddress(emailaddress);
  284. return true;
  285. }
  286. catch (FormatException)
  287. {
  288. return false;
  289. }
  290. }
  291.  
  292. string customerEmailAddress = "[email protected]";
  293. customerEmailAddress.IsValidEmailAddress()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement