I have this email validation attribute
[EmailAddress]
[StringLength(50, ErrorMessage = "Email is too long!")]
public string Email { get; set; }
And it works fine, but the problem is when I type the email, and then erase it, the form still asks me to insert correct email even though the field is not required.
it will accept empty string or exact an email id
"^$|^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$"
Answer
Related
I have created an ASP.NET Web application using razor pages (not controllers) and this uses the Individual accounts from Identity.
The whole purpose of my site is that an admin logs in and uploads a document, they then press a button that sends an email to the receipting user to say they have a document to view.
I was wondering with microsoft identity, is there a possibility to use multiple emails? For example, putting a semi-colon between emails so that when the email is sent, it goes to multiple people.
The code im using behind the button to send the email is as follows:
using (var smtp = new System.Net.Mail.SmtpClient("ip address here"))
{
var emailMessage = new MailMessage();
emailMessage.From = new MailAddress("email#gmail.com");
emailMessage.To.Add(email);
emailMessage.Subject = "New Document Avaialable!";
emailMessage.Body = "You have a new purchase order available to view on the Portal!";
await smtp.SendMailAsync(emailMessage);
}
Fix as follows:
In the register.cshtml, there is an InputModel class that looks something along the lines of:
public class InputModel
{
[Required]
[DataType(DataType.Text)]
[Display(Name = "User Name")]
public string UserName { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
}
First of all, I removed the [EmailAddress] from above the email.
Then instead of a semi-colon in the email text box, i put a comma between each email address and it worked.
I am struggling to validate an empty EditorFor using Data-Annotation, have tried using jquery on a client side. Somehow I feel it's better to validate it from the back end than front end. Have a look and help me to improve my logic error. The idea I want this error to validate when user leaves the #EditorFor() showing error image.
// Model
[Required(ErrorMessage = "This field is required")]
public string Email { get; set; }
//View
script type='text/javascript'>
$(function () {
//When the blur event occurs from your Textbox (you lose focus)
$('#textEmail').blur(function () {
var email = document.getElementById("textEmail").value;
var expr = /^([\w-\.]+)##((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!expr.test(email)) {
alert("Invalid email address.");
}
else {
alert("Ok");
}
});
});
It can be achieved without JQuery by using MVC's Data Annotation properly.
For data annotation, write error message above the property as below:
[Required(ErrorMessage = "This field is required")]
public string Email { get; set; }
Some other annotations are also available along with Required annotation. To validate the valid email address below annotation can be used:
[Required(ErrorMessage = "This field is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
public string Email { get; set; }
As we know when you create an ASP.NET Core appp using Individual User Authentication project template, it creates a default ResetPassword.cshtml view. In that View I need to set logged in user name input tag as readonly. But doing so is throwing the following validation error. If I don't make it readonly the below screen successfully allows user to change password.
Question: Why the following validation error on form submit - when the UserName input tag is set to readonly? I know that if the input tag is disabled then form submit does not submit the input tag's value (also explained by #AdamBellaire here). It seems [Required] annotation in public string UserName { get; set; } is somehow conflicting with readonly attribute of input tag.
public class ResetPasswordViewModel
{
[Required]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
public string Code { get; set; }
}
UPDATE
ResetPassword.cshtml: [It's a default View created by VS2017. Only change is that I added readonly attribute in input tag below]
#model ResetPasswordViewModel
...
<input asp-for="#User.Identity.Name" class="form-control" readonly />
...
In this situation, it really makes no sense to expect #User.Identity.Name in the view to bind to UserName in the view model.
It would seem that the code the IDE generated is wrong. Maybe a messed up scaffolding template somewhere, who knows.
You need to change your asp-for to equal UserName. See below.
<input asp-for="UserName" class="form-control" readonly />
Glad this helped!
Currently I am using following code for email validation but
it does validate the dsgf#g mail id please help me
[Required(ErrorMessage = "Please Enter Email Id")]
[Display(Name = "Email-Id")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }
EmailAddress attribute will mark as valid the dsgf#g because it is a completely valid email address, so there is nothing wrong with that method. Consider the example username#localhost for example.
If it is not suits you then you can use regular expression ti set your own rule for validation. Try to use 'RegularExpression' attribute instead, something like:
[RegularExpression("^[^#\s]+#[^#\s]+(\.[^#\s]+)+$", ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }
or
[RegularExpression(#"^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Invalid Email Address")]
public string Cust_Email { get; set; }
the email is completely valid.
if you want to validate, simply don't use regex for validation. Send him a code to this email-address that he has to enter. email-addresses can now contain characters like ä,ö,ü,à,... this could be really difficult to match the correct one..
if you really want to validate it using regex you could take the RFC822 standard then:
you will find here: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html (have fun with this - didn't want to post, it's too long)
What about an extension method.
public static bool IsValidEmail(this string email)
{
bool rt = false;
try
{
var mail = new System.Net.Mail.MailAddress(email);
rt = mail.Host.Contains(".");
}
catch { }
return rt;
}
I am using Asp.net MVC to create a password and confirm password field. I am currently using the remote attribute to check if password and confirm password are the same, however remote will only call a function if the box it is applied to is changed.
I've looked through previous posts going back to last year and found the most common suggestion to be the compare attribute, however it is now deprecated.
I assume there is a pre-built solution for this problem.
This is from the model
[Remote(UserController.ActionNameConstants.PasswordMatch,
UserController.NameConst, AdditionalFields = "ConfirmPassword",
ErrorMessage = "The passwords do not match")]
[MinLength(8, ErrorMessage="Minimum password length is 8")]
[DisplayName("Password"), RequiredIf("Id == 0",
ErrorMessage="Password is required")]
public string Password { get; set; }
[DisplayName("Confirm Password"), RequiredIf("Id == 0",
ErrorMessage = "Confirm password is required")]
public string ConfirmPassword { get; set; }
This is in the controller
[HttpGet]
public virtual JsonResult PasswordMatch(string password,string confirmPassword)
{
return this.Json(password ==
confirmPassword,JsonRequestBehavior.AllowGet);
}
Compare is not depricate you can still use [Compare("Property name to compare with")]... it's in "System.ComponentModel.DataAnnotations" namespace.