validation - form still submitted after return false javascript -
validation - form still submitted after return false javascript -
i'm having little problem validation thing in javascript.
<form action="insert.php" id="form" name="form" method="post" onsubmit="return validate()"> <pre> vul hier de/het e-mail adres(sen) in <textarea name="email" rows="5" cols="50"></textarea><br> typ hier de e-mail <textarea name="text" rows="5" cols="50"></textarea><br> <input type="submit" name="submit" value="submit"> </pre> </form>
as can see here, i've got 2 textareas. in upper one, you're supposed come in 1 or multiple email addresses underneath eachother, , in bottom textarea you're supposed compose email itself. then, when click on submit, it'll send email specified email addresses.
now, i've made validation both textareas:
function explodearray(emailid, delimiter) { temparray = new array(1); var count = 0; var tempstring = new string(emailid); while (tempstring.indexof(delimiter) > 0) { temparray[count] = tempstring.substr(0, tempstring.indexof(delimiter)); tempstring = tempstring.substr( tempstring.indexof(delimiter) + 1, tempstring.length - tempstring.indexof(delimiter) + 1 ); count = count + 1 } temparray[count] = tempstring.replace("\r", ""); homecoming temparray; } function validate() { var emailid = document.form.email; var delimiter = "\n"; var emailarray = explodearray(emailid.value, delimiter); var textid = document.form.text; var length = emailarray.length, element = null; (var = 0; < length; i++) { emailvar = emailarray[i]; if (emailvar == null) { alert("email-adres bestaat niet") emailid.focus() homecoming false } if (emailvar == "") { alert("email-adres veld leeg") emailid.focus() homecoming false } if (checkemail(emailvar) == false) { emailvar.value = "" alert("ongeldig e-mail adres"); emailvar.focus() homecoming false } } if ((textid.value == null) || (textid.value == "")) { alert("e-mail textveld leeg") textid.focus() homecoming false } document.getelementbyid("form").submit(); homecoming true } function checkemail(hallo) { if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(hallo)) { homecoming true } homecoming false }
(i copied lots of irrelevant code well, sorry that, copied whole thing in case...)
now does work is:
-it won't submit when both textareas empty;
-it won't submit when email addresses valid but bottom textarea empty;
what doesn't work is:
-the form still submits when email addresses invalid, when bottom textarea still empty.
i've been trying figure out hours perchance wrong here, googled , checked stackoverflow, not find anything. tell me i'm doing wrong here?
thanks in advance.
you using emailvar.focus();
won't execute.
here, fixed: live demo
if (checkemail(emailvar) == false) { alert("ongeldig e-mail adres"); emailid.focus(); homecoming false; }
javascript validation
Comments
Post a Comment