javascript - form validation using java script -
following form validates using javascript,but not working. still takes action,instead of showing error.i cant figure out wrong code. seems fine. can show me mistakes?
<script type='text/javascript'> function formvalidator(){ var name = document.getelementbyid('name'); var alpha = /^[a-za-z]+$/; if(!alpha.test.(name.value)){ alert('please provide valid name'); name.focus; return false; } 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('please provide valid email address'); email.focus; return false; } } </script> <div id="contact_form"> <p>stay connected.</p> </div> <form name="contact" method="post" action="sendmail.php" onsubmit='return formvalidator();'> <fieldset> <label for="name" id="name_label">your name *</label> <input type="text" name="name" id="name" size="50" value="" class="text-input" required /> <label for="email" id="email_label">your email address *</label> <input type="text" name="email" id="email" /> <br /> <input type="submit" name="submit" class="button btn btn-primary btn-info" id="submit_btn" value="send" onclick='javascript:formvalidator();'/> </fieldset> </form> dont want use html5 validation.
this error in console
syntaxerror: missing name after . operator [break on error] if(!alpha.test.(name.value)){ so use
if (!filter.test(name.value)) instead of if(!alpha.test.(name.value)){ remove .
Comments
Post a Comment