Disable Required attribute from code behind - c#

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?

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");

Related

How to apply a specific style to an invalid field?

Suppose I have a field like this:
<div class="form-group">
<label for="fullname" asp-for="UserName">Username</label>
<input class="form-control"
type="text" name="username"
required asp-for="UserName">
<span asp-validation-for="UserName"></span>
</div>
inside my controller I added this error:
ModelState.AddModelError("username", "username already registered");
Now, I want apply the focus on the input field and also I want apply a red border, is possible do this only via MVC?
When the model validation fails and you return to the same view, the framework will add the input-validation-error CSS class to the input elements, for which the validation failed. In your case, you are adding the error message to the UserName field, so your UserName field input will get this new CSS class.
<input class="form-control input-validation-error" type="text" name="username"
required="" id="UserName" value="some value">
You can add any sort of styling you want to this CSS class as needed.
.input-validation-error
{
border-color:red;
}
Now setting the focus on a specific input field is a little tricky. The server code cannot do that. You have to do that on client side. But if there are multiple fields in the form with failed validation, which one you want to focus ? The first one, second one ? last one ?
Here is a quick sample to focus on the first input element with input-validation-error CSS class. The JavaScript code is executed on the jquery document.ready event.
$(function () {
$("input.input-validation-error").first().focus();
});
If you want to style the validation error message rendered by the validation helper (<span asp-validation-for="UserName"></span>), follow the same approach. When validation fails, the framework will change the CSS class of this span element to field-validation-error (from field-validation-valid). So all you have to do is, add the needed style definitions to that CSS class.
.field-validation-error
{
color:red;
}

Html attribute based on server side variable?

I have something like this.
<input type="text">
I want enable/disable it based in certain variable value in server side.
I have tried this.
<input type="text" <%= DisableServiceInfo ? "disabled":"" %>/>
but not working.
I know this can be done.
<input type="text" disabled="<%= DisableServiceInfo ? "disabled":"invalid value" %>"/>
but this is not a valid mark-up. Because the only valid way to enable control is to remove disabled attribute.
I am not asking how value can be supplied based on variable but how attribute in injected
Please don't answer the ways to set it server side or by javascript. I just want to know if it is possible in this way?
Is this webforms? If you want to do this by the book, you can manually add an Id, and Runat="Server". Once you've done that, your control can be manipulated in your code-behind.
If your Id is ServiceInfo, you could do:
ServiceInfo.Attributes["disabled"] = "disabled";

Create a textbox of type="number"

I have a custom control which uses System.Windows.Form.Textbox as the underlying text box.
I want to change that type to type="number" however I don't see a way to do this.
The end goal is to have an input field which, when clicked on a smartphone browser, will give you only numeric keys to enter rather than the full keyboard.
It is possible to restrict the input to numbers only using JavaScript, but that is not what I want. I specifically need the keyboard to open with only numbers on it. If there is an alternative way to achieve this than setting type="number" then that should work as well.
You can add type="number" to an ASP.NET TextBox and it will render with that attribute.
<asp:TextBox ID="TextBox1" runat="server" type="number" />
Depending on ASP.NET version it will either render with both type="text" and type="number" (v2.0 - v3.5) or just type="number" (v4.0+).
If you wanted to add this conditionally in your code-behind, you could:
TextBox1.Attributes.Add("type", "number");

Get HTML element attribute value using Request[]

I have a page where many postback clicks are there.
In this page many input elements there, which values are i am geting from server side using Request[]. like below
.aspx
<input id="txtRefTypeCtrlType_3" name="txtRefTypeCtrlType_3" lastvalue="4" CTRLtype="4" style="display: none;">
.cs
string strCTRLtype = Request["txtRefTypeCtrlType_3"];
now i have a scenario, where i need to get the CTRLtype attribute value.
Can we get the attribute value using Request[] or something else.
Please advise.
Thanks.
No, you can't get the attribute value on the server side because it isn't posted. The only real way of achieving this is to create a hidden field with the attribute value and use that:
<input type="hidden" name="ctrlType_3" value="4" />
Codebehind:
string attrValue = Request["ctrlType_3"];

display asterisk instead of no value in password field

I would like to display asterisk instead of no value in password field.
When I am setting up the field as type=text then I can see the plain text. When I set the type as password then I am getting blank field.
Is there a way to display the value as asterisks?
<input type="text" id="txtPwd" runat="server" />
This is the field I have.
Thanks in advance, Laziale
You can use the HTML5 placeholder attribute:
<input type="password" placeholder="●●●●●●●●●">
Example: http://jsfiddle.net/AJ2kn/
If you want the user's text input to be replaced by asterisks you need to use a password input—a text input just won't do it:
<input type="password" id="txtPwd" runat="server" />
However, the default value will be blank (this applies to text input as well). If you want to initialize this to a default value, you can do it like this:
<input type="password" value="dummy" id="txtPwd" runat="server" />
Note: I highly recommend not putting a users' password as the default value of a password field! It may compromise your users' password.
If you want to display some asterisks as placeholder text when the field is empty you can do that with with the placeholder attribute (again, this applies to text input as well):
<input type="password" placeholder="*****" id="txtPwd" runat="server" />

Categories