0

I wonder if I can execute onlick function only after JS validation any ideas?

I have a form which has e-amil and phone mandatory filed and i would like to check them before form is submitted , even more if everything is ok another div onclick="$('#dialog-links-blocks').show()" should be display with a message Thank you! How can I make it to be shown only after validation !

<script type="text/javascript">// <![CDATA[
function validateForm()
{
var x=document.forms["myform"]["phone"].value;
var y=document.forms["myform"]["email"].value;
if (x==null || x=="")
  {
  alert("Укажите номер телефона!");
  return false;
  }
if (y==null || y=="")
  {
  alert("Укажите email!");
  return false;
  }

}
// ]]></script>


<iframe name="hiddenFrame" width="320" height="240"></iframe>
<form id="validation" action="http://listovki.md/send_form_email.php" method="POST" name="myform" enctype="multipart/form-data" onsubmit="return validateForm()" target="hiddenFrame">
    <table><colgroup> <col width="122" /> <col width="260" /> </colgroup>
    <tbody>
    <tr>
    <td><label>имя</label></td>
    <td><input type="text" name="family" /></td>
    </tr>
    <tr>
    <td><label>почта</label></td>
    <td><input type="email" name="email" /></td>
    </tr>
    <tr>
    <td><label>gsm</label></td>
    <td><input class="gsm" type="tel" name="phone" /></td>
    </tr>
    <tr>
    <td colspan="2"><input class="aplicaAcumBt" onclick="$('#dialog-links-blocks').show()" type="submit" name="submit" value="отправить" /></td>
    </tr>
    </tbody>
    </table>
    </form>
5
  • 2
    you can't because the click event of the submit button will fire before the submit event of the form is fired.... you can rearrange the code so that the code is executed after the validation Commented Jan 24, 2014 at 10:33
  • 1
    if you want to go with inlined event handler(not recommended) then try <form id="validation" action="http://listovki.md/send_form_email.php" method="POST" name="myform" enctype="multipart/form-data" onsubmit="if(!validateForm()){return false;}$('#dialog-links-blocks').show();" target="hiddenFrame"> and remove the onclick handler Commented Jan 24, 2014 at 10:35
  • validation is ok but div block doesn't show up :( Commented Jan 24, 2014 at 10:38
  • may is better to arrange it in one Jquery code? Commented Jan 24, 2014 at 10:55
  • You could use jQuery validation plugin to validate your inputs right after they were filled. (jqueryvalidation.org) Commented Jan 24, 2014 at 12:48

1 Answer 1

1

Why not show div only if validation is true? why show only on click

    <script type="text/javascript">// <![CDATA[
    function validateForm()
    {
    var x=document.forms["myform"]["phone"].value;
    var y=document.forms["myform"]["email"].value;
    if (x==null || x=="")
      {
      alert("Укажите номер телефона!");
      return false;
      }
    if (y==null || y=="")
      {
      alert("Укажите email!");
      return false;
      }
    // Validation is true here
    $('#dialog-links-blocks').show()
    }
    </script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.