Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
1.9 KiB

  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. namespace UDDI.Web
  7. {
  8. /// <summary>
  9. /// Summary description for validators.
  10. /// </summary>
  11. public class EmailValidator : BaseValidator
  12. {
  13. public const string Expression = "(?<user>[^@]+)@{1}(?<host>.+)";
  14. private const string csName = "ValidateEmail";
  15. private Regex regexp;
  16. private Control control;
  17. private bool resolvehost;
  18. public bool ResolveHost
  19. {
  20. get{ return resolvehost; }
  21. set{ resolvehost=value; }
  22. }
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. public EmailValidator()
  27. {
  28. //
  29. // TODO: Add constructor logic here
  30. //
  31. regexp = new Regex( Expression );
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="e"></param>
  37. protected override void OnInit( EventArgs e )
  38. {
  39. base.OnInit( e );
  40. }
  41. /// <summary>
  42. ///
  43. /// </summary>
  44. /// <param name="e">EventArguments</param>
  45. protected override void OnLoad( EventArgs e )
  46. {
  47. control = FindControl( this.ControlToValidate );
  48. if( null==control )
  49. {
  50. throw new Exception( "The Control Specified can not be found: '" + this.ControlToValidate + "'" );
  51. }
  52. base.OnLoad( e );
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. /// <returns>Boolean indicating that the email address is valid</returns>
  58. protected override bool EvaluateIsValid()
  59. {
  60. Match m = regexp.Match( ((TextBox)control).Text );
  61. try
  62. {
  63. if( m.Success )
  64. {
  65. if( ResolveHost )
  66. {
  67. System.Net.IPHostEntry host = System.Net.Dns.Resolve( m.Groups[ "host" ].Value );
  68. if( null!=host )
  69. return true;
  70. }
  71. else
  72. {
  73. return true;
  74. }
  75. }
  76. }
  77. catch{}//swallow and return false.
  78. return false;
  79. }
  80. }
  81. }