Hanady
Hanady

Reputation: 789

Disable Required attribute from code behind

I have two html input in one form, and I am setting them to required as follows:

<input id="email" name="textfield36" type="text" class="input3" runat="server" required="required" />
<input id="frEmail" name="textfield36" type="text" class="input3" runat="server" required="required" />

I need to set the Required attribute to false to one of the two inputs from c#. For example:

if (language == "English")
{
    frEmail.Attributes.Add("required", "false");
}
else
{
    email.Attributes.Add("required", "false");
}

This is causing an issue, because if the language is English, then the user only needs to fill the specified fields and the way it's happening now is that he's obliged to fill them all and vice versa. Note that on load I'm hiding the fields not relating to the language.

Can anyone help on this?

Upvotes: 1

Views: 7502

Answers (1)

gilly3
gilly3

Reputation: 91637

The required attribute is a Boolean attribute. That means the value is ignored. It's presence on the element all that matters. You need to remove the required attribute, not change its value.

email.Attributes.Remove("required");

Upvotes: 2

Related Questions