When I add any regular expression pattern in MVC for textarea it's not working. When I click on submit button the wrong value is added to database
My View Code:
#Html.TextAreaFor(model => model.MyEntity.Item, new { #required = "required", #class = "form-control english_only", #placeholder = "Item", #maxlength = "120", #Pattern = #"[^A-Za-z0-9]" })
My Model Code:
[StringLength(150)]
public string Item{ get; set; }
This is just for any other who have this problem...
Based on Developers comments:
HTML5 <textarea> element does not support pattern attribute
so the solution is by MVC built in validation (with jquery.validate.js) and jquery.validate.unobtrusive.js)
So this problem solve by change my model to be:
[StringLength(150)]
[RegularExpression("^[0-9]*$", ErrorMessage = "UPRN must be numeric")]
public string Item{ get; set; }
and also include the jquery.validate.js in my view or partial view
Ore simply change my html helper to be text box instead of text Area as following:
#Html.EditorFor(model => model.Item, new { htmlAttributes = new { #class = "form-control english_only", #placeholder = "Only english text", #required = "required", #Pattern = #"^[a-zA-Z0-9$#$!%*?&#^-_. +]+$", #title = "please insert english text only", #autocomplete= "off", #maxlength = "120" } })
Related
This is my model's date property:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy.mm.dd}")]
[Required(ErrorMessage = "You need to fill this")]
[DataType(DataType.DateTime, ErrorMessage = "Type proper date")]
[Display(Name = "Weight Date")]
public DateTime? WeightDate { get; set; }
This is how I render bootstrap's datepicker
#Html.EditorFor(x => x.WeightDate, new { htmlAttributes = new { #id="WeightDate" #class = "form-control datepicker", #required = "required" }})
#Html.ValidationMessageFor(m => m.WeightDate, "", new { #class = "text-danger" })
Validation seems to working (I can see "You need to fill this" when entering empty datepicker value). The problem is that I can see other messages, which I would don't want to see (or at least change their messages):
When I enter invalid data, I am getting error (from client side):
The field Weight date must be a date.
When I submit invalid data, I am getting error (from server side):
The value '202441.02.03' is not valid for Weight date.
How can I change those messages?
This question already has answers here:
MVC4 input field placeholder
(10 answers)
Closed 7 years ago.
In my ViewModel for my partial view I have Email defined like this:
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
In the view I have this:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control"})
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
I would like something like 'example#company.com' to appear in the textbox as a placeholder or example. But right now it is empty.
You can set a placeholder attribute (HTML5) to the form element. Browsers which supports this feature will add a placeholder text to your textbox.
#Html.TextBoxFor(s=>s.Email, new { #class="form-control", placeholder="sample#ss.com"})
Use this to add a placeholder attribute to your HTML:
#Html.TextBoxFor(m => m.Email, new {#class = "form-control", placeholder = "example#company.com"})
I have the following properties
public SelectList ListActivities { get; set; } // Will load all hobbies i,e football, golf etc and will be displayed in a dropdown
public List<string> SelectedActivities { get; set; } // Trying to set with multiple selected values.
This is my view.
<div class="col-lg-11">
#Html.DropDownListFor(m => m.UserDetails.SelectedActivities, Model.UserDetails.ListActivities, "Please Select", new { #class = "form-control", multiple = "multiple", id = "listActivities" })
</div>
The issue I have is when I selected more then one option from the ActivitiesDropdown and press submit on my page and go back to the controller the SelectedActivities is null.
Can one shed some light on this please?
For multi-select you should use Html.ListBoxFor and not a Html.DropDownListFor because Html.DropDownListFor returns a single-selection select element.
So for this to work just change your view to:
<div class="col-lg-11">
#Html.ListBoxFor(m => m.UserDetails.SelectedActivities, Model.UserDetails.ListActivities, "Please Select", new { #class = "form-control", id = "listActivities" })
</div>
I have a required string for my model and I can submit the form and still leave the textbox blank and the model state is being returned as valid so I have no idea what I did wrong. This is the only control on my page that is doing this. Here is my code:
Model
[Required(ErrorMessage = "Requirements are Required.")]
[DataType(DataType.MultilineText, ErrorMessage = "Requirements is Required.")]
[StringLength(200, ErrorMessage = "200 Characters is the maximum allowed for requirements.")]
public string Requirements { get; set; }
View
<div class="form-group">
#Html.LabelFor(model => model.Requirements, "Requirements:", htmlAttributes: new { #class = "col-sm-4 control-label" })
<div class="col-sm-8">
#Html.TextAreaFor(model => model.Requirements, htmlAttributes: new { #rows = "10", #cols = "50", #class = "form-control" })
#Html.ValidationMessageFor(model => model.Requirements)
</div>
Edit:
As #Stephen Muecke noted, I cant reproduce your case neither, it should be enough Required attribute.
My answer below somehow Workaround as said by #MajkeloDev.
You should put MinimumLength = 10 for your field. number 200 indicates maximum length for your string
[StringLength(200, MinimumLength = 10, ErrorMessage = "200 Characters is the maximum allowed for requirements.")]
Note: MinimumLength = 10 is being some number for a meaningful input. For non empty string it is enough MinimumLength = 1
I am trying to get #String.Format("{0:0.00}",Model.CurrentBalance) into this #Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance" })
I just want the currency to show up as .00 inside of my textbox but am having no luck. Any ideas on how I do this?
string.format("{0:c}", Model.CurrentBalance) should give you currency formatting.
OR
#Html.TextBoxFor(model => model.CurrentBalance, new { #class = "required numeric", id = "CurrentBalance", Value=String.Format("{0:C}",Model.CurrentBalance) })
#Html.TextBoxFor(model => model.CurrentBalance, "{0:c}", new { #class = "required numeric", id = "CurrentBalance" })
This lets you set the format and add any extra HTML attributes.
While Dan-o's solution worked, I found an issue with it regarding the use of form-based TempData (see ImportModelStateFromTempData and ExportModelStateToTempData). The solution that worked for me was David Spence's on a related thread.
Specifically:
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
public decimal? Price { get; set; }
Now if you use EditorFor in your view the format specified in the annotation should be applied and your value should be comma separated:
<%= Html.EditorFor(model => model.Price) %>