I would like to force uppercase on a textbox in my view. So when user type something in the textbox, either the text is immediately converted in uppercase or (an alternative) the text is converted in uppercase when the form is submitted and the model is populated with data (in the action controller). Maybe there are some CSS or jquery solutions but I prefer some MVC solutions (if possible).
Here is my view:
@model Domain.Entities.ProjectTechnology
<script src="../../Scripts/Admin/_AddProjectTechnology.js" type="text/javascript"></script>
<div class="editor-label">Technologies</div>
<div class="editor-field">
@using (Ajax.BeginForm("_AddProjectTechnology", new AjaxOptions { HttpMethod = "POST",
UpdateTargetId = "RemoveProjectTechnology",
InsertionMode = InsertionMode.Replace,
OnComplete = "AddProjectTechnologyCompleted" })) {
@Html.ValidationSummary(true)
@Html.HiddenFor(m => m.ProjectID)
@Html.EditorFor(m => m.TechnologyID)
@Html.ValidationMessageFor(m => m.TechnologyID)
}
</div>
And here is my model:
public class ProjectTechnology
{
public int ProjectID { get; set; }
public string TechnologyID { get; set; }
}
The textbox in question is this line: @Html.EditorFor(m => m.TechnologyID)
How can I proceed ?
Thanks.
HTML5 Pattern Validation As an aid to form validation we can use HTML5 input patterns to flag when an input is not valid. In the JavaScript examples, the pattern to enforce only uppercase (assuming a single word - no spaces or punctuation) is: <input ... pattern="[A-Z]*" ...>
The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.
upper() method to convert a user input string to uppercase, e.g. uppercased = user_input. upper() . The str. upper() method returns a copy of the string with all the cased characters converted to uppercase.
IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.
The easiest way is IMO:
@Html.EditorFor(m => m.TechnologyID, new { htmlAttributes = new { @style = "text-transform:uppercase" } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With