I am using ASP.NET MVC 4 and I am looking for an attribute for SSN Validation.
[Required(ErrorMessage = "Social Security is Required")]
[SSN]
public string SSN { get; set; }
I know this doesn't work, but this is what I am looking for.
Can anyone Help
You need to use a Regex.
Try something like this.
[Required(ErrorMessage = "SSN is Required")]
[RegularExpression(#"^\d{9}|\d{3}-\d{2}-\d{4}$", ErrorMessage = "Invalid Social Security Number")]
public string SSN { get; set; }
You need to use a Regex.
[RegularExpression(#"[1-9]{1}\d{2}-\d{2}-\d{4}$", ErrorMessage = "Invalid Social Security Number")]
This Regex also check's that the first digit must not be zero.
Related
I have the following model:
public class Human
{
[Required]
[StringLength(50)]
[Display(Name = "First Name")]
public String FirstName { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Last Name")]
public String LastName { get; set; }
// 50 more required fields not shown for brevity
}
If a user doesn't fill out a required field, I would like the error message to be "This field is required". However, I don't want to annotate every single one with [Required(ErrorMessage = "This field is required")] because that takes a lot of copy/pasting.
Is there a way to tell ASP.NET MVC that all required fields in ONLY THIS CLASS should have the error message "This field is required"?
I did not find a way to accomplish this. I think it is not possible.
This question already has answers here:
Email address validation using ASP.NET MVC data type attributes
(12 answers)
Closed 8 years ago.
I want to force users to register with their e-mail addresses DataType.EmailAddress doesn't work.
public class RegisterModel
{
[Required]
[Display(Name = "E-mail")]
[DataType(DataType.EmailAddress)]
public string UserName { get; set; }
...
}
Any help is appreciated.
If your wanting validation for an email address, you need the [EmailAddress] attribute (NET 4.5)
[Required]
[Display(Name = "E-mail")]
[EmailAddress] // Add this
public string UserName { get; set; }
or for NET 4.0 you can use a [RegularExpression] attribute (this one is taken from jquery-validate 1.9.0)
[Required]
[Display(Name = "E-mail")]
[RegularExpression(#"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$", ErrorMessage="Please enter a valid email adress")]
public string UserName { get; set; }
Note the [DataType] attributes are used by #Html.EditorFor() to render the type attribute used by browsers (e.g type="email")
You can use the "EmailAddress" annotation:
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string UserName { get; set; }
EDIT:
For MVC 4 and below, you can use regex expression to do validation on Email address fields.
See more here:
http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
I tried the following code for digits-only validation for a contact number validation in Mvc web app.
[RegularExpression(#"/(^\(\d{10})?)$/", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
But the validation expression is not working.
For the contact number I want to only accept digits. It can be either a 10 digit mobile number or a land-line number.
If don't have any restrictions other than numbers only, this should fit:
[RegularExpression(#"^\d+$", ErrorMessage = "Please enter proper contact details.")]
[Required]
[Display(Name = "Contact No")]
public string ContactNo { get; set; }
/ / is javascript way to build a regular expression literal object. In .NET you should not use it.
Try the following:
#"^\((\d{10}?)\)$"
or if you want exactly 10 digits:
#"^(\d{10})$"
This worked for me:
[RegularExpression(#"^[0-9]{10}", ErrorMessage = "Please enter proper contact details.")]
Is there a way using MVC data validation attributes to validate client side if two fields on my model are equal.
I have two fields:
[Required(ErrorMessage = "*")]
[Email(ErrorMessage = "*")]
public string Email { get; set; }
[Required(ErrorMessage = "*")]
[Email(ErrorMessage = "*")]
public string ConfirmEmail { get; set; }
I want to be able to add an attribute that those two fields should be equel and if not an validatio error will appear. Is there a way to do so?
Thank you.
Yep - for example:
[Compare("Email", ErrorMessage = "The email and confirmation do not match.")]
Hope that helps.
Take a look at the CompareAttribute
[Compare("Email", ErrorMessage = "The email and confirmation email do not match.")]
public string ConfirmEmail { get; set; }
I've been playing around data annotations in MVC2 and am curious if there is an annotation to compare 2 properties (ie. password, confirm password)?
If you are using ASP.Net MVC 3, you can use System.Web.Mvc.CompareAttribute
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Compare("Password")]
public string PasswordConfirm { get; set; }
Here you go: http://www.dotnetguy.co.uk/post/2010/01/09/Property-Matching-With-Data-Annotations.aspx
Edit:
New link: http://www.dotnetguy.co.uk/post/2010/01/09/property-matching-with-data-annotations/
System.Web.Mvc.CompareAttribute has been deprecated.
I was able to modify to work like this:
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
There's not one built in, however, you can make your own. See this link, which shows the "PropertiesMustMatchAttribute" that does just what you're looking for.