Wednesday, 15 July 2015

javascript - Where's the error in js validation -



javascript - Where's the error in js validation -

i'm working on form validation right now. onsubmit sumbmitting when there errors. i'm not sure problem is. i'm thinking it's around regex portions of code, not sure. where's error in validation script?

function test_form() { var username = document.regform.username.value; var firstname = document.regform.firstname.value; var lastname = document.regform.lastname.value; var email = document.regform.email.value; var email2 = document.regform.email2.value; var password = document.regform.password.value; var password2 = document.regform.password2.value; var birthday = document.regform.birthday.value; var birthmonth = document.regform.birthmonth.value; var birthyear = document.regform.birthyear.value; var tou = document.regform.tou.checked; var regspec = /!|@|#|\$|%|-|_|&|\*|\(|\)|\+|\[|\]|\{|\}|:|;|'|"|\||\\|,|<|>|\.|\?|\/|\^/; var reguser = /!|@|#|\$|%|&|\*|\(|\)|\+|\[|\]|\{|\}|:|;|'|"|\||\\|,|<|>|\.|\?|\/|\^/; var error = false; // test fields empty , dob format , tou acceptance if(username === "" || firstname === "" || lastname === "" || email === "" || email2 === "" || password === "" || password2 === "" || tou == false || birthday == "--day--" || birthyear == "--year--" || birthmonth == "--month--") { error = true; } // test fields applicable special characters if(firstname.match(regspec) || lastname.match(regspec)) { error = true; } if(username.match(reguser) || password.match(reguser) || password2.match(reguser)) { error = true; } // test emails format , match if(email.indexof("@") == -1 || email.indexof(".") == -1 || email2.indexof("@") == -1 || email2.indexof(".") == -1) { error = true; } // test passwords match if(password != password2) { error = true; } // test email match if(email != email2) { error = true; } // send homecoming error; }

how using function. stop submit of form must homecoming false. if function used onsubmit handler if there error function returns true, mean form submit – ojay 16 mins ago

this commenter right. changed boolean noerror , made sure errors set false. works great now. need php validation :) thanks.

javascript

No comments:

Post a Comment