Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  191] [ 3]  / answers: 1 / hits: 16256  / 12 Years ago, thu, june 7, 2012, 12:00:00

I need a way to submit a form only if email is valid. If email is NOT valid then show ONLY an alert ( below you can see my code ).



JAVASCRIPT:



 function validateEmail(email) { 
var re = /^(([^<>()[]\.,;: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 re.test(email);
}


function continueornot() {
if(validateEmail(document.getElementById('emailfield').value)){
// ok
}else{ alert(email not valid); return false;}
}


HTML:



<form method=post action=register.php enctype=multipart/form-data>
<table>
<tr><td>Email:</td></tr>
<tr><td><input type=text size=30 name=email id=emailfield> </td></tr>
<tr><td>Password:</td></tr>
<tr><td><input type=password size=30 name=password> </td></tr>
</table>
<input name=steptwo value=Create Account type=submit onclick=continueornot();/>
</form>


The problem is that the form is submitted even if the email is NOT valid.



How can I resolve this problem ?


More From » javascript

 Answers
29

You should attach return continueornot(); to the onsubmit event of the form, not the button's click event.



See this fiddle: http://jsfiddle.net/NdSmu/


[#85095] Tuesday, June 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stacie

Total Points: 476
Total Questions: 92
Total Answers: 102

Location: Bosnia and Herzegovina
Member since Tue, Mar 29, 2022
2 Years ago
stacie questions
Fri, Jun 26, 20, 00:00, 4 Years ago
Thu, Jan 23, 20, 00:00, 4 Years ago
Fri, Aug 30, 19, 00:00, 5 Years ago
Fri, Aug 2, 19, 00:00, 5 Years ago
;