Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set required attribute on Html.Textbox

I'm looking to create a Bootstrap styled textbox, specifically, based on the exact example below:

<input class="span3" type="email" required> 

Here's what I have so far:

@Html.TextBox("CustomerEmail", null, new { @class = "input-xlarge", type = "email", required = "required" }) 

However, required = "required" clearly doesn't return just required.

So, my question is, is there any way I can force it to return required like in the first example above when using Html.Textbox?

like image 893
LiamGu Avatar asked Jan 11 '13 11:01

LiamGu


People also ask

How do I make certain fields required in HTML?

1. Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.

How do I add a required symbol in HTML?

Indicating form controls as required using asterisks (*) Asterisk (*) next to a form control's label usually indicates it as "required". Oftentimes, this asterisk's purpose is then explained somewhere else on the page.

How do I make text area needed?

The required attribute is a boolean attribute. When present, it specifies that a text area is required/must be filled out (in order to submit the form).

How do you make a required false in HTML?

However, if one is handling mouse events, for instance, using JS, they can set the "required" attribute to "True" or "False" using the ". prop()" method. e.g.


2 Answers

i think you should use like this

 @Html.TextBoxFor(model => model.Name, new { @class = "text", type = "email", required = "required" }) 

i think this will help you.

like image 82
Rajpurohit Avatar answered Sep 21 '22 08:09

Rajpurohit


Try

new { required = string.Empty} 

As an aside, according to the W3C docs, required is a Boolean attribute, and I quote:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Therefore

required required="required" required="" 

all mean the same thing.

like image 28
Pauline Avatar answered Sep 21 '22 08:09

Pauline