0

I have some javascript that checks the forms user input. I click Submit after checking return false but it still submit.I want to stop form submit.

javascript code

<script>
function validation(){
    var emailFilter=/^.+@.+\..{2,3}$/;
    var str=document.form.email.value;

    if (!(emailFilter.test(str))) { 

        document.getElementById("emailStatus").innerHTML = '<p class="notify-valid"> <p class="notify-valid" style="font-family: Kanit;"><i class="icon ion-close-round" style="color:#FF0004;!important"></i> Wrong Email</p></p>';

        document.getElementById('hideMessage').style.display = 'block';

        return false;
    }

}
</script>

html code

     <form name="form" action="action.php" method="post" onsubmit="return validation()">
        <div class="form-group">
        <label class="sr-only" for="contact-email">Your Email : </label>

        <input type="text" name="email" placeholder="Enter Email..." class="contact-email form-control" id="contact-email">
        <div class="message" id="emailStatus">                                        
            <div id="hideMessage" style="display:none"></div>
            </div>
        </div>

        <div class="form-group">
        <label class="sr-only" for="contact-message">Your message ซ</label>
            <textarea name="message" placeholder="message" class="contact-message form-control" id="contact-message"></textarea>
        </div>

        <button type="submit" class="btn">Submit</button>

    </form>
  • Have not figure out why yet but document.getElementById('hideMessage') returns a null for me so the style tag causes a javascript error. – Bindrid Jan 12 '16 at 08:13
  • Possible duplicate of [HTML form action and onsubmit issues](http://stackoverflow.com/questions/16262797/html-form-action-and-onsubmit-issues) – cweitat Jan 12 '16 at 08:15
  • Are you sure you entered the if in the validate function? Maybe add some console.log inside and after the if to see where it gets – Gavriel Jan 12 '16 at 10:10
  • it work but not return false. Now I m delete document.getElementById("emailStatus").innerHTML = '

    Wrong Email

    '; it's Work!! Thank u all
    – Kittiphat Rueangphaophan Jan 12 '16 at 10:16

0 Answers0