I have this form here and i dont want them to go to the next page without certain selections
<form method="post" action="step2/" id="form1">
....
....
....
<input type="submit" class="submit notext" value="Next" />
and here is my jquery
$('.submit').click(function(e) {
var business = $(".business_type_select").find('.container strong').text();
alert(business);
if(business == "Select Business Type"){
alert("BusinessBusinessBusiness");
e.preventDefault;
return false;
}
});
any ideas what i am missing to get this to stop submitting
I have this form here and i dont want them to go to the next page without certain selections
<form method="post" action="step2/" id="form1">
....
....
....
<input type="submit" class="submit notext" value="Next" />
and here is my jquery
$('.submit').click(function(e) {
var business = $(".business_type_select").find('.container strong').text();
alert(business);
if(business == "Select Business Type"){
alert("BusinessBusinessBusiness");
e.preventDefault;
return false;
}
});
any ideas what i am missing to get this to stop submitting
Share Improve this question asked May 10, 2011 at 20:48 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges5 Answers
Reset to default 8Try using the submit
event:
$("#formID").submit(function(e) {
var business = $(".business_type_select").find('.container strong').text();
alert(business);
if(business == "Select Business Type"){
alert("BusinessBusinessBusiness");
return false;
}
});
Also, the e.preventDefault()
is a function, but is redundant as the return false
will work just the same.
preventDefault
is a function - use e.preventDefault()
.
There are sometimes issues with .preventDefault()
in IE try adding this:
if (e.preventDefault) // checks to see if the event has a preventDefault method
e.preventDefault();
else
e.returnValue = false;
$("#formID").submit(function(e) {
var business = $(".business_type_select").find('.container strong').text();
alert(business);
if(business == "Select Business Type"){
alert("BusinessBusinessBusiness");
e.preventDefault();
return false;
}
});
If your asp MVC razor form looks something like this:-
You can use (document) ID to validate form values using JavaScript. JavaScript validations fire prior to validations done using HTML Helpers ( @ValidationFor etc)..
@using (Html.BeginForm("MyRequestAction", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
code goes here...
}
$(document).submit(function (e) {
var catVal = $("#Category").val();
if (catVal == "") {
alert("Please select Category!");
return false;
}
if (catVal == "--Select One--") {
alert("Please select Category!");
return false;
}
});