Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  8] [ 5]  / answers: 1 / hits: 16427  / 8 Years ago, wed, may 4, 2016, 12:00:00

I have a from.



@using (Html.BeginForm())
{
// From content
<div class=form-group btn-group>
<button type=submit class=btnPrimaryStyle btn col-sm-3 col-xs-12 pull-left>Save</button>
</div>
}


Everything is working fine. I have a dropdwon which is out of the form. I want to check, if dropdown value is selected,then form can be submitted otherwise it will not submit. In short, the dropdown selection is required and it is out of the form. I can check the value of dropdwon weather it is selected or not with jquery or javascript. My question is how i prevent to submit the form if value is not selected?


More From » jquery

 Answers
31

With jQuery it's simple:



$(#your-form-id).submit(function (e) { // note that it's better to use form Id to select form
if($(#YourDropDownID).val() == 0 || $(#YourDropDownID).val() == '') // here you check your drop down selected value
{
e.preventDefault(); // here you stop submitting form
}
}


To set id to your form use this overload:



Html.BeginForm(null, null, FormMethod.Post, new { id = your_form_id })

[#62312] Sunday, May 1, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;