Why does calling this Javascript function submit my form? -
i have form validation script. unless conditions met, ambiguous function associated form.onclick
returns false (form doesn't submit). have function upon error append error message errorlog div.
function adderror (msg) { msg = document.createtextnode(msg); br = document.createelement("br"); errorlog.append(msg); errorlog.append(br); alert("yes"); }
if error found, calling function submits form. script doesnt enter function, submits data , refreshes. realize there error in function, cant seem figure out.
edit: more of code
form.onsubmit = function () { if (namecheck(fname, lname)) return true; return false; };
function checks if "name" field valid:
function namecheck (fname,lname) { if (("/\d/").test(fname) || ("/\d/").test(lname)) { adderror('your name cannot contain numbers.'); valid = false; } }
you're not stopping form submitting.
use e.preventdefault()
temporarily stop form submitting, if form correct use this.submit()
finish submitting form
form.onsubmit = function (e) { e.preventdefault(); if (namecheck(fname, lname)) { this.submit(); return true; } return false; };
also, namecheck
never resolve true, doesnt return anything. should this
function namecheck (fname,lname) { if (("/\d/").test(fname) || ("/\d/").test(lname)) { adderror('your name cannot contain numbers.'); return false; } return true; }
Comments
Post a Comment