Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
742
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@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. public static bool IsValidEmailAddress(this string s)
  149. {
  150. 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])?");
  151. return regex.IsMatch(s);
  152. }
  153.  
  154. myEmailString.IsValidEmailAddress();
  155.  
  156. var myPattern = Regex.EmailPattern;
  157.  
  158. /// <summary>
  159. /// Validates the string is an Email Address...
  160. /// </summary>
  161. /// <param name="emailAddress"></param>
  162. /// <returns>bool</returns>
  163. public static bool IsValidEmailAddress(this string emailAddress)
  164. {
  165. var valid = true;
  166. var isnotblank = false;
  167.  
  168. var email = emailAddress.Trim();
  169. if (email.Length > 0)
  170. {
  171. // Email Address Cannot start with period.
  172. // Name portion must be at least one character
  173. // In the Name, valid characters are: a-z 0-9 ! # _ % & ' " = ` { } ~ - + * ? ^ | / $
  174. // Cannot have period immediately before @ sign.
  175. // Cannot have two @ symbols
  176. // In the domain, valid characters are: a-z 0-9 - .
  177. // Domain cannot start with a period or dash
  178. // Domain name must be 2 characters.. not more than 256 characters
  179. // Domain cannot end with a period or dash.
  180. // Domain must contain a period
  181. isnotblank = true;
  182. valid = Regex.IsMatch(email, Regex.EmailPattern, RegexOptions.IgnoreCase) &&
  183. !email.StartsWith("-") &&
  184. !email.StartsWith(".") &&
  185. !email.EndsWith(".") &&
  186. !email.Contains("..") &&
  187. !email.Contains(".@") &&
  188. !email.Contains("@.");
  189. }
  190.  
  191. return (valid && isnotblank);
  192. }
  193.  
  194. /// <summary>
  195. /// Validates the string is an Email Address or a delimited string of email addresses...
  196. /// </summary>
  197. /// <param name="emailAddress"></param>
  198. /// <returns>bool</returns>
  199. public static bool IsValidEmailAddressDelimitedList(this string emailAddress, char delimiter = ';')
  200. {
  201. var valid = true;
  202. var isnotblank = false;
  203.  
  204. string[] emails = emailAddress.Split(delimiter);
  205.  
  206. foreach (string e in emails)
  207. {
  208. var email = e.Trim();
  209. if (email.Length > 0 && valid) // if valid == false, no reason to continue checking
  210. {
  211. isnotblank = true;
  212. if (!email.IsValidEmailAddress())
  213. {
  214. valid = false;
  215. }
  216. }
  217. }
  218. return (valid && isnotblank);
  219. }
  220.  
  221. public class Regex
  222. {
  223. /// <summary>
  224. /// Set of Unicode Characters currently supported in the application for email, etc.
  225. /// </summary>
  226. public static readonly string UnicodeCharacters = "À-ÿp{L}p{M}ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü«»€₣äÄöÖüÜß"; // German and French
  227.  
  228. /// <summary>
  229. /// Set of Symbol Characters currently supported in the application for email, etc.
  230. /// Needed if a client side validator is being used.
  231. /// Not needed if validation is done server side.
  232. /// The difference is due to subtle differences in Regex engines.
  233. /// </summary>
  234. public static readonly string SymbolCharacters = @"!#%&'""=`{}~.-+*?^|/$";
  235.  
  236. /// <summary>
  237. /// Regular Expression string pattern used to match an email address.
  238. /// The following characters will be supported anywhere in the email address:
  239. /// ÀàÂâÆæÇçÈèÉéÊêËëÎîÏïÔôŒœÙùÛûÜü«»€₣äÄöÖüÜß[a - z][A - Z][0 - 9] _
  240. /// The following symbols will be supported in the first part of the email address(before the @ symbol):
  241. /// !#%&'"=`{}~.-+*?^|/$
  242. /// Emails cannot start or end with periods,dashes or @.
  243. /// Emails cannot have two @ symbols.
  244. /// Emails must have an @ symbol followed later by a period.
  245. /// Emails cannot have a period before or after the @ symbol.
  246. /// </summary>
  247. public static readonly string EmailPattern = String.Format(
  248. @"^([w{0}{2}])+@{1}[w{0}]+([-.][w{0}]+)*.[w{0}]+([-.][w{0}]+)*$", // @"^[{0}w]+([-+.'][{0}w]+)*@[{0}w]+([-.][{0}w]+)*.[{0}w]+([-.][{0}w]+)*$",
  249. UnicodeCharacters,
  250. "{1}",
  251. SymbolCharacters
  252. );
  253. }
  254.  
  255. w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*
  256.  
  257. public bool IsEmail(string text)
  258. {
  259. 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})$");
  260. return regex.IsMatch(text);
  261. }
  262.  
  263. public bool VailidateEntriesForAccount()
  264. {
  265. if (!(txtMailId.Text.Trim() == string.Empty))
  266. {
  267. if (!IsEmail(txtMailId.Text))
  268. {
  269. Logger.Debug("Entered invalid Email ID's");
  270. MessageBox.Show("Please enter valid Email Id's" );
  271. txtMailId.Focus();
  272. return false;
  273. }
  274. }
  275. }
  276. private bool IsEmail(string strEmail)
  277. {
  278. Regex validateEmail = new Regex("^[\W]*([\w+\-.%]+@[\w\-.]+\.[A-Za-z] {2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+@[\w\-.]+\.[A-Za-z]{2,4})[\W]*$");
  279. return validateEmail.IsMatch(strEmail);
  280. }
  281.  
  282. if (!System.Text.RegularExpressions.Regex.IsMatch("<Email String Here>", @"^([w.-]+)@([w-]+)((.(w){2,3})+)$"))
  283. {
  284. MessageBox.show("Incorrect Email Id.");
  285. }
  286.  
  287. public class LoginViewModel
  288. {
  289. [EmailAddress(ErrorMessage = "The email format is not valid")]
  290. public string Email{ get; set; }
  291.  
  292. public static bool ValidateEmail(string str)
  293. {
  294. return Regex.IsMatch(str, @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*");
  295. }
  296.  
  297. using System.Text.RegularExpressions;
  298. if (!Regex.IsMatch(txtEmail.Text, @"^[a-z,A-Z]{1,10}((-|.)w+)*@w+.w{3}$"))
  299. MessageBox.Show("Not valid email.");
  300.  
  301. string EmailPattern = @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*";
  302. if (Regex.IsMatch(Email, EmailPattern, RegexOptions.IgnoreCase))
  303. {
  304. Console.WriteLine("Email: {0} is valid.", Email);
  305. }
  306. else
  307. {
  308. Console.WriteLine("Email: {0} is not valid.", Email);
  309. }
  310.  
  311. string patternEmail = @"(?<email>w+@w+.[a-z]{0,3})";
  312. Regex regexEmail = new Regex(patternEmail);
  313.  
  314. ^[w!#$%&'*+-/=?^_`{|}~]+(.[w!#$%&'*+-/=?^_`{|}~]+)*@((([-w]+.)+[a-zA-Z]{2,4})|(([0-9]{1,3}.){3}[0-9]{1,3}))$
  315.  
  316. ^(([^<>()[]\.,;: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,}))$
  317.  
  318. public static bool IsValidEmail(string email)
  319. {
  320. 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})$");
  321. return !String.IsNullOrEmpty(email) && r.IsMatch(email);
  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