Here's the skinny: In the below code #new-org-btn
is an input
of type submit
and I want it to only submit its containing form
if the click
function reaches the else
statement. As of now, it is submitting even it doesn't reach the else
statement.
$('#new-org-btn').click(function() {
var newOrgName = $('#new-org-ipt').val();
console.log(newOrgName.length);;
if (newOrgName.length == 0)
{
$('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
}
else if (newOrgName.indexOf('-') == -1)
{
$('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");
}
else
{
$('#new-org-output-msg').css('color', 'green');
$('#new-org-form').submit();
}
});
I know that $('#new-org-form').submit(function() { return false; });
would disable the form from doing anything when it submits, but the problem is that if I do
$('#new-org-btn').click(function() {
var newOrgName = $('#new-org-ipt').val();
console.log(newOrgName.length);;
if (newOrgName.length == 0)
{
$('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot be blank.");
$('new-org-form').submit(function() { return false; });
}
else if (newOrgName.indexOf('-') == -1)
{
$('#new-org-output-msg').css('color', 'red').text("New organization not submitted. Name cannot contain any '-' characters.");
$('new-org-form').submit(function() { return false; });
}
else
{
$('#new-org-output-msg').css('color', 'green');
$('#new-org-form').submit();
}
});
then when I actually enter the else statement and want to submit the form as usual, it may have been deactivated from a previous call to the function. I therefore need a way to "reactivate" the form. How do I do that?