How to validate Uppercase in MVC - asp.net-mvc

I'm developing a MVC project, I want to validate it for some text filed are uppercase. How can I do it?
I'm using this **text-transform:uppercase** but I want to validate in model
[DisplayName("Last Name :")]
[Required]
[StringLength(50, MinimumLength = 3, ErrorMessage = "Last Name must be between 3 and 50 characters!")]
public string LastName { get; set; }

[RegularExpression(#"[A-Z]{3,50}$",
ErrorMessage = "Only uppercase Characters are allowed.")]
use regular expression attribute.

Related

'...DataType' does not contain a definition for 'Email'

I'm using Identity, and the IdentityUsers properties are the next: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework.identityuser_properties%28v=vs.108%29.aspx
Well, the problem is I have in the AspNetUsers table "Email" and "PasswordHash", but appears the error'System.ComponentModel.DataAnnotations.DataType' does not contain a definition for 'Email' and 'PasswordHash')
if I put "Datatype.Email" instead of "DataType.EmailAddress" and "DataType.PasswordHash" instead of "DataTtype.Password".
public class RegisterViewModel
{
[Required]
[DataType(DataType.Email)]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The password must have at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.PasswordHash)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.PasswordHash)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "Different passwords.")]
public string ConfirmPassword { get; set; }
}
The error is telling you exactly what you need. DataType is an enumeration which only allows specific values. These in turn are used for validation in the MVC world to ensure that you have a valid EmailAddress in the field, whatever it happens to be called.
[DataType(DataType.Email)] // Not Valid
[DataType(DataType.EmailAddress)] // Valid
[DataType(DataType.PasswordHash)] // Not Valid
[DataType(DataType.Password)] // Valid
For the full list of valid values, see Intellisense or this URL:
https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.datatype%28v=vs.110%29.aspx

asp.net MVC 4 SSN Validation

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.

Substitution arguments in ValidationAttribute.ErrorMessage

In an ASP.NET MVC 4 app, the LocalPasswordModel class (in Models\AccountModels.cs) looks like this:
public class LocalPasswordModel
{
[Required]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
The above code contains two substitution arguments in the ErrorMessage string:
ErrorMessage = "The {0} must be at least {2} characters long."
Can someone tell me where the values that get substituted into that string come from? More generally, is there anything approximating official documentation that describes how parameter substitution works in this context?
For StringLengthAttribute, the message string can take 3 arguments:
{0} Property name
{1} Maximum length
{2} Minimum length
These parameters unfortunately do not seem to be well documented. The values are passed in from each validation attribute's FormatErrorMessage attribute. For example, using .NET Reflector, here is that method from StringLengthAttribute:
public override string FormatErrorMessage(string name)
{
EnsureLegalLengths();
string format = ((this.MinimumLength != 0) && !base.CustomErrorMessageSet) ? DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : base.ErrorMessageString;
return String.Format(CultureInfo.CurrentCulture, format, new object[] { name, MaximumLength, MinimumLength });
}
It is safe to assume that this will never change because that would break just about every app that uses it.

Mvc validation regular expression only numbers?

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.")]

Enter conditional values either or validation in asp.net mvc2

In my form I have phone and mobile number fields I need to guarantee that the user enters at least one of the numbers. I don’t want to force the user to enter both numbers.
I looked at this previous post but it didn’t work for me
conditional either or validation in asp.net mvc2
public class ContractViewModel
{
[Required(ErrorMessage = "Please enter First Name")]
[StringLength(50, ErrorMessage = "Length of First Name must be less than 50")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please enter Last Name")]
[StringLength(50, ErrorMessage = "Length of Last Name must be less than 50")]
public string LastName { get; set; }
[Required(ErrorMessage = "Please enter Phone Number")]
[StringLength(10,ErrorMessage = "Length of Phone No. must be less than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid home phone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Please enter Mobile No.")]
[StringLength(10, ErrorMessage = "Length of Mobile No. must be les than 10")]
[RegularExpression("^[0-9]{10}$", ErrorMessage = "Please enter a valid mobile")]
public string Mobile { get; set; }
}
How would I go about doing this?

Resources