www.webdeveloper.com
Results 1 to 4 of 4

Thread: Javascript validation problem in form

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Javascript validation problem in form

    Hi Friends.,

    I have a validation code for an email and checkbox field on a form. The code check that all required fields (email and checkbox) are completed and when they are not showing the alert.

    But when it is all completed, when you press the send button, it does not execute the action "mailer.php".

    I leave the validation code here, maybe i have missed something and therefore do not run the action.

    Code:
    <script type="text/javascript">
    function validar()
    {
    	
    var email = document.getElementById('email');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
    alert('Por favor compruebe la dirección de email');
    email.submit;
    return false;
    }
    	
    	elemento = document.getElementById("checkbox");
    if( !elemento.checked ) alert('Por favor acepte el aviso legal'); {
      return false;
    }
    
    	return true;
    }
    
    </script>
    I really appreciate your help
    Thanks a lot.

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You misplaced a curly bracket:

    Code:
    if( !elemento.checked ) alert('Por favor acepte el aviso legal'); >>{<<
      return false;
    }
    Right now the function is always returning false and preventing form submission.

  3. #3
    Join Date
    Nov 2006
    Location
    Oakland
    Posts
    500
    What's email.submit; for?
    Maybe you meant email.focus();

    if( !elemento.checked ) {
    alert('Por favor acepte el aviso legal');
    return false;
    }
    How is the JavaScript form validation handler invoked from your HTML?

    You should have
    <form name="myform" action="mailer.php" onsubmit="return validar();">
    ....
    ....
    <input type="submit" name="btnsubmit" value="Send">
    </form>

  4. #4
    Join Date
    Oct 2012
    Posts
    2
    Hi guys,

    Thanks to both! I have solved the problem thanks your help. This is the final code:

    Regards from Spain

    Code:
    <script type="text/javascript">
    function validar()
    {
    	
    var email = document.getElementById('email');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
    alert('Por favor compruebe la dirección de email');
    email.submit;
    return false;
    }
    	
    var elemento = document.getElementById("checkbox");
    if( !elemento.checked ) {alert('Por favor acepte el aviso legal'); 
    return false;
    }
    
    	return true;
    }
    
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles