cyberfido
cyberfido

Reputation: 5

Submit does not fire jquery function

I have this JS script that shoudl check all the fileds of the form. The problem is that this function isn't called and I don't understand why.

$j(document).ready(function(){  

function validateEmail(email) {         
    var pattern = "/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/";
    return email.match(pattern);  
} 

$j('#contact_submit').submit(function(e){
    if($j('#contact_name').val() == "") {
        e.preventDefault();
        alert("Devi inserire un nome per inviare l'email");
        return false;
    }
    if($j('#contact_email').val() == "") {
        e.preventDefault();
        alert("Devi inserire un contatto email valido per inviare l'email");
        return false;
    }
    if(validateEmail($j('#contact_email').val()) == null)
    {
        e.preventDefault();
        alert("L'email inserita non è in un formato valido");
        return false;
    }
    if($j('#contact_text').val() == "") {
        e.preventDefault();
        alert("Devi inserire un testo per inviare l'email");
        return false;
    }
    return true;
});

});

This is the form

<form method="post" name="form" action="email.php">
<table width="550" border="0" align="center" cellpadding="4" cellspacing="0">
      <td align="right">Nome*</td>
      <td><input maxlength="50" id="contact_name" name="contact_name"  size="30" /></td>
    </tr>
    <tr>
      <td align="right">Città</td>
      <td><input maxlength="50" id="contact_city" name="contact_city" size="30" /></td>
    </tr>
    <tr>
      <td align="right">N. Telefono</td>
      <td><input maxlength="20" id="contact_tel" name="contact_tel" size="30" /></td>
    </tr>
    <tr>
      <td align="right">E-mail*</td>
      <td><input maxlength="30" id="contact_email" name="contact_email"  size="30" /></td>
    </tr>
    <tr>
      <td align="right">Testo *</td>
      <td><textarea cols="50" id="contact_text" name="contact_text" rows="5" class="tinymce"></textarea></td>
    </tr>
    <tr>
      <td colspan="2" align="center">
      <p>I campi contrassegnati con l'asterisco (*) sono obbligatori</p>
      <p>   <input id="contact_submit" type="submit" value="Invia" />
            <input id="contact_reset" type="reset" value="Annulla" />
        </p></td>
    </tr>
</table>
</form>

Simply when i click on submit button, the form is called and the data sent via email avoiding the jquery function. What's wrong?

Thanks in advance

Upvotes: 0

Views: 84

Answers (4)

Barmar
Barmar

Reputation: 781068

It should be:

$("form[name=form]").submit(...

The submit event happens on the form, not the submit button.

Upvotes: 1

Sven
Sven

Reputation: 70863

Your form does not have an id. Because of this it does not get your submit-function attached.

The submit button only has a click event, no submit event.

Upvotes: 0

Edwin Alex
Edwin Alex

Reputation: 5108

There is no submit() handler for the button. Use click() handler.

$j('#contact_submit').click(function(e){

});

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

Add the id to the form instead of the button.

<form id="contact_submit" method="post" name="form" action="email.php">

Upvotes: 1

Related Questions