I am using ASP.NET MVC 5 and I have many classes with decimal fields.
I know that I can use:
[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]
public decimal MyDecimal { get; set; }
in order to format the display of the number, but this means entering the attribute for every field.
I need a method to format these numbers globally.
Related
In my ViewModel, I have the following System.ComponentModel.DataAnnotations on a property that contains USD currency:
[DisplayFormat(DataFormatString = "{0:C2}")]
[Range(0.01, 100000, ErrorMessage = "Payment amount is required between .01 and $100,000.")]
[DataType(DataType.Currency)]
[DisplayName("Payment Amount")]
public Double PrinAmount { get; set; } = 0.00;
When I enter a value of $10.005, I get the following validation model error from the ModelState.IsValid check:
The value '$10.005' is not valid for Payment Amount.
When I enter a value 10.005, the ModelState.IsValid is equal to true.
What do I need to do to modified the validation to capture both formats as invalid?
You can use Regular Expression;
[RegularExpression(#"^\d+\.\d{0,2}$")]
the DataAnnotations above ensures 2 digits.
I am using below atrribute to format numbers (appending commas in values). Now i want to remove zero after decimal. how i can achieve the same with below property.
[DisplayFormat(DataFormatString = "{0:N}")]
public decimal? Y01Amount { get; set; }
Any help is appreciated.
[DisplayFormat(DataFormatString = "{0:n0}")]
With above attribute both cases can be achieved in single DisplayFormat
[DisplayFormat(DataFormatString = "{0:0.##}")]
I need to control a field with a min length if someone enters a value but if they don't enter anything in, I don't want the form to tell them there is a min value.
This is what I have:
[Required]
[StringLength(15, ErrorMessage = "Please supply at least {2} characters.", MinimumLength = 3)]
[Display(Name = "Last name on account or first part of the company's name")]
public string LastName { get; set; }
I just need for it to allow blanks also or if data is entered, require it to be a min of 3 characters..
Any suggestions?
The problem is with the validation logic of the StringLength attribute, that returns true also for the string with null value, here the implementation:
public override bool IsValid(object value)
{
this.EnsureLegalLengths();
int num = value == null ? 0 : ((string) value).Length;
if (value == null)
return true;
if (num >= this.MinimumLength)
return num <= this.MaximumLength;
else
return false;
}
Also the Required attribute that you used is not helping :-).
Anyway the only thing you can do for your scenario is to create a custom attribute to validate LastName with the logic that you need, here a link to an MVC3 example, or you can try googling, there is a lot of examples and is not hard to implement.
Is it possible to both validate, that the provided data is in the form of a phone number AND trim it down to just the numbers in the validator?
Input: (902) 837-2832
Output: VALID: YES, 9028372832
Or do I have to convert the input to the number-only format after the fact?
Add a property to your model with only a getter that returns the stripped down version of the property that is bound to the input. Put your validation attribute on that property.
public string PhoneNumber {get;set;}
[Required(ErrorMessage="Phone number is required.")]
[RegularExpression(#"\d{10}", ErrorMessage="Phone number is invalid.")]
public string PhoneNumberValue
{
get
{
var temp = PhoneNumber
temp = Regex.Replace(temp, #"[^0-9]", "");
temp = temp.Length == 11 && temp.StartsWith("1")
? temp.Substring(1) : temp;
}
set
{
// I can't remember off the top of my head if MVC model
// binding requires a setter or not. If so, just leave this
// empty. Otherwise you can remove it entirely.
}
}
Then, in your view, just render the alternate validation message.
#Html.LabelFor(x=>x.PhoneNumber)
#Html.TextBoxFor(x=>x.PhoneNumber)
#Html.ValidationMessageFor(x=>x.PhoneNumberValue)
Here is an example how to validate with Regular expression:
[Required(ErrorMessage="Phone Number is required")]
[RegularExpression("^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage="Not a valid number")]
public string PhoneNumber { get; set; }
We may use Trim method of string to clean the phone number and get only digits.
char[] charsToTrim = { '(', ' ', ')', '-'};
string phoneNumber = "(123)-345-6789";
string result = banner.Trim(charsToTrim);
Finally here is a post that explains Enabling Validation using DataAnnotations in more detail
I have my ViewModel on which I've defined my properties; one of them is a DateTime field defined as:
[DisplayName("Data")]
[DataType(DataType.Date)]
[Required(ErrorMessage="Selezionare una data")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}" )]
public DateTime? DtNews { get;set; }
When I submit the form I always receive the error msg "The value '13/07/2011' is not valid for DtNews" because the system swaps days and months ... I need to have dd/mm/yyyy format.
You could set the culture in your web.config to some culture for which the date format is dd/MM/yyyy. For example that's the case with it-IT:
<globalization culture="it-IT" uiCulture="it-IT"/>
By default the culture is set to auto. This means that ASP.NET uses the browser settings in order to deduce the culture of the client. So if the browser is configured to use en-US, then the format for dates will be MM/dd/yyyy.
Another possibility is to set the format for the current thread culture:
var dtfi = (DateTimeFormatInfo)Thread.CurrentThread.CurrentCulture.DateTimeFormat.Clone();
dtfi.ShortDatePattern = "dd/MM/yyyy";
dtfi.DateSeparator = "/";
Thread.CurrentThread.CurrentCulture.DateTimeFormat = dtfi;
Thread.CurrentThread.CurrentUICulture.DateTimeFormat = dtfi;