I've got a series of EditFors that cover the months of the year. Here's what I have for one in the model :
[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
[Display(Name = "November")]
[StringLength(4, ErrorMessage = "Text (November) entered cannot exceed 3 characters plus decimal")]
[Range(0.00, 1.00, ErrorMessage = "(November) Number must be in between 0.00 and 1.00")]
public decimal Month11 { get; set; }
If someone enters 0.20 or 0.2, everything's nice, but if they only enter .2 or .20, the textbox turns red, indicating there's an error, and when the form is submitted, I get the following error:
The field November must be a number.
Nowhere am I creating that particular error message.
3 Questions:
How can I change that error message (so that I can include the msg to include a leading zero? (preferred)
How can I auto format the textbox to include a leading zero if it doesn't exist (maybe when focus of textbox is changed to another)?
I listed #1 as preferred, but I'd really like to go with the easiest method to implement
[RegularExpression(@"[0-9]*$", ErrorMessageResourceName = "The field November must be a number.", ErrorMessageResourceType = typeof(Strings))][DataType(DataType.Currency)]
[Display(Name = "November")]
public int November{ get; set; }
I changed it to 'String' instead of 'Strings' and didn't give me the error that 'String' did..
I also changed the ErrorMessageResourceName to 'The January data must be a number in the '0.00' format.'
Then, though it compiled, it crashed when it got to that part of the view - "The resource type 'System.String' does not have an accessible static property named 'The January data must be a number in the '0.00' format.'."
elmoWatson
Participant
1055 Points
607 Posts
MVC - JQuery - error message problem
Jan 04, 2013 02:47 PM|LINK
I've got a series of EditFors that cover the months of the year. Here's what I have for one in the model :
[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)] [Display(Name = "November")] [StringLength(4, ErrorMessage = "Text (November) entered cannot exceed 3 characters plus decimal")] [Range(0.00, 1.00, ErrorMessage = "(November) Number must be in between 0.00 and 1.00")] public decimal Month11 { get; set; }If someone enters 0.20 or 0.2, everything's nice, but if they only enter .2 or .20, the textbox turns red, indicating there's an error, and when the form is submitted, I get the following error:
Nowhere am I creating that particular error message.
3 Questions:
I listed #1 as preferred, but I'd really like to go with the easiest method to implement
Specs
Member
618 Points
152 Posts
Re: MVC - JQuery - error message problem
Jan 04, 2013 02:57 PM|LINK
I believe you can try something like this:
http://mirko.blogs.aspiant.com/2009/06/aspnet-mvc-form-validation-error-with.html
ramramesh
Member
458 Points
158 Posts
Re: MVC - JQuery - error message problem
Jan 04, 2013 03:01 PM|LINK
Hai
Try this way
[RegularExpression(@"[0-9]*$", ErrorMessageResourceName = "The field November must be a number.", ErrorMessageResourceType = typeof(Strings))][DataType(DataType.Currency)] [Display(Name = "November")] public int November{ get; set; }elmoWatson
Participant
1055 Points
607 Posts
Re: MVC - JQuery - error message problem
Jan 04, 2013 04:07 PM|LINK
I changed it to 'String' instead of 'Strings' and didn't give me the error that 'String' did..
I also changed the ErrorMessageResourceName to 'The January data must be a number in the '0.00' format.'
Then, though it compiled, it crashed when it got to that part of the view - "The resource type 'System.String' does not have an accessible static property named 'The January data must be a number in the '0.00' format.'."
elmoWatson
Participant
1055 Points
607 Posts
Re: MVC - JQuery - error message problem
Jan 10, 2013 05:52 PM|LINK
My fix was to change the EditFors to TextBoxFor - and then set them up like this:
@Html.TextBoxFor(model => model.FiscalFTEModel.Allocation.Month08, new { maxlength = "4" })