-2

I want to validate SMTP host and port. Please let me know how can I do this? I want to smtp.gmail.com or smtp-mail.outlook.com. But now my code is allowing numbers also.

public int Smtp_Id{ get; set; }

[Display(Name="Mail From")]
[Required(ErrorMessage = "Please enter senders mail address.")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Smtp_mailfrom { get; set; }

[Display(Name = "SMTP Host")]
[Required(ErrorMessage = "Please enter host name.")]     
public string Smtp_Host { get; set; }

[Display(Name = "User Name")]
[Required(ErrorMessage = "Please enter username.")]
[MaxLength(50,ErrorMessage="Username Should not be more than 50 charachters.")]
public string Smtp_username { get; set; }

[Display(Name = "Password")]
[MaxLength(50, ErrorMessage = "Password Should not be more than 50 charachters.")]
[Required(ErrorMessage = "Please enter password.")]
public string Smtp_password { get; set; }

[Display(Name = "SMTP Port")]
[Required(ErrorMessage = "Please enter Port.")]
public int Smtp_Port { get; set; }
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Gurudev Kumar
  • 160
  • 1
  • 14
  • 1
    Possible duplicate of [mvc4 url validation](http://stackoverflow.com/questions/15249105/mvc4-url-validation) - It's not exactly the same issue, but the answer has a link to an extension that would help you with your issue. – mtaanquist Oct 05 '16 at 13:44
  • No Mr. Rory That is only for URL but here i need validation for smtp host & port – Gurudev Kumar Oct 06 '16 at 11:54

1 Answers1

0

For host name you need to regular expression and for the port no you need something custom like as below it will helpful to you.

    [Display(Name = "SMTP Host")]
    [Required(ErrorMessage = "Please enter host name.")]
    [RegularExpression("smtp.gmail.com|smtp-mail.outlook.com")]
    public string Smtp_Host { get; set; }


    [Display(Name = "SMTP Port")]
    [Required(ErrorMessage = "Please enter Port.")]
    [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a natural number")]
    [Range(1, 9999, ErrorMessage = "Port no must between 1 to 9999")]     
    public int Smtp_Port { get; set; }
Pragnesh Khalas
  • 2,908
  • 2
  • 13
  • 26
  • Thank you Mr. Pragnesh i'll try this code in my application tomorrow. Then after i'll give feedback. But your regex will only validate gmail & outlook Host.. If i want to use any other then ?? – Gurudev Kumar Oct 05 '16 at 16:50
  • Do you list of those valid hosts? If yes then just add on RegularExpression data annotation. And suppose it is dynamic list then you need to add custom data annotation. – Pragnesh Khalas Oct 06 '16 at 07:22
  • Thank you Mr. Pragnesh I need one more help.. Can you tell me how can i store a byte array to localstorage in MVC application. Actually i want to store one image in local. cookie allow only 4 KB data. if i wanted to store data like 500 kb or 1 MB then ??? – Gurudev Kumar Oct 06 '16 at 11:42
  • you can use html5 local storage it will able to you store 10mb data in the browser memory. below link will help you http://stackoverflow.com/questions/19158887/saving-and-loading-an-image-from-localstorage – Pragnesh Khalas Oct 06 '16 at 13:19