Advertisement
Guest User

Untitled

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