问题
Model:
[Display(Name = "City"]
[Required]
[RegularExpression(@"^(?!\d$).*$"]
[StringLength(20,MinimumLength = 2]
public string City { get; set; }
Form:
@Html.LabelFor(x => x.City, new { @class = "control-label" })
@Html.TextBoxFor(x => x.City, new {id="city" })
Script:
<script>
$(document).ready(function () {
$("#identificationForm").submit(function (e) {
var required=document.getElementById("city").required;
console.log(required);
// e.preventDefault();
});
});
</script>
I want to remove required property if some condition is met.Unable to do this this way.How can i achieve this?
回答1:
JavaScript is case sensitive.
Use
document.getElementById("city").required = false;
Demonstration
Be careful that your code can't work as you try to access the element before it exists. Put your script after the element if you don't execute the code on an event :
<input type="text" id="city" required>
<script>
if(somecondition is true){
document.getElementById("city").required = false;
}
</script>
Note also that you can't change this in a submit function and expect your form to be submitted because it's too late : this event handler won't be called if the required field is not filled !
回答2:
You can use:
document.getElementById("city").removeAttribute("required");
or with jQuery
$('#city').removeAttr('required')
回答3:
You shoud do this:
if(somecondition is true)
{
var city = document.getElementById("city");
city.removeAttribute('required');
}
回答4:
By the time the On Submit
function is to remove the required attribute, the form would have already passed validation. The steps are first you have to disable validation then you will be able to submit the form with the required fields empty. Next remove the required attributes and finally manually invoke validation via $.validator.unubtrusive.parse(form)
and this will now validate the form by invoking the other validation types such as string length
and formatting
, everything else except the required attributes. Check if form.valid() then submit() else
do nothing.
回答5:
In php its very easy, this will not validate your form while submitting,
<input type="submit" onclick="return confirm('Are you sure?')" value="Abort Test" formnovalidate>
回答6:
Not sure if anyone has figured out a better way to do this yet and it might not be the most efficient way to do it but you could use PHP to get this done. If the condition you are looking to meet is known before the page loads then you can have this:
<input type="text" id="city"
<?php
if (condition_is_met) {
echo ">";
} else {
echo "required>";
}
?>
回答7:
If you put your text fields in a form, it is possible to overwrite the required field with a special attribute called novalidate. This is the syntax: <form novalidate> .. </form>
Here is an example:
<form novalidate>
Zip code: <input type="text" name="zipcode" pattern="[0-9]{5}" required><br/>
<input type="submit">
</form>
The novalidate Attribute Here is a link with two live demos using novalidate attribute.
回答8:
Or in the moment that form is submitted you can disabled it if it's null:
if($("#City").val().trim().length == 0)
$("#City").attr("disabled", true);
来源:https://stackoverflow.com/questions/18111915/remove-required-property-from-input-field-on-form-submit