Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force uppercase on a Html.TextboxFor

Tags:

asp.net-mvc

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.

like image 816
Bronzato Avatar asked Nov 06 '11 10:11

Bronzato


People also ask

How do you force uppercase in HTML?

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

How do you force lowercase in HTML?

The toLowerCase() method converts a string to lowercase letters. The toLowerCase() method does not change the original string.

How do you convert input to uppercase?

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.

What is the difference between HTML TextBox vs HTML TextBoxFor?

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.


1 Answers

The easiest way is IMO:

@Html.EditorFor(m => m.TechnologyID, new { htmlAttributes = new { @style = "text-transform:uppercase" } })
like image 191
leifbattermann Avatar answered Oct 08 '22 19:10

leifbattermann