UrbanDweller Posted January 20, 2012 Share Posted January 20, 2012 Hey, I have been playing around with forms a Javascript validation scripts for the past week and have hit a few brick walls when coming to the onsubmit event. Below I have an anonymous function for the submission of the form. // JavaScript Document var valid = "valid"; function validateForm(){ var f = document.getElementById("infoForm"); f.onsubmit = function() { errorCheck(f); if(valid == "valid"){ return true; }else { document.getElementById("form_1").className = "activeForm"; document.getElementById("form_4").className = "disabledForm"; valid = "valid"; previousPage(); return false; } } } function errorCheck(f){ for(var i = 0; i < f.elements.length; i++) { var e = f.elements[i]; if(f.elements[i].type == "text" && f.elements[i].className == "text" || f.elements[i].type == "text" && f.elements[i].className == "error"){ f.elements[i].className = "error"; valid = "invalid"; }else{ continue; } } return; } window.onload = function(){ addHandlers(); validateForm(); } Bugs/question The main issue is that my form will never submit, it will only submit if variable valid == "valid" else it returns false and returns user to first form. I check each text input on the fly and change its css class to "pass" if its value is not empty else it will change to error or stay as text if not touched. So im pretty sure the issue isn't the error checker. In my week of learning javascript form submission has been my greatest enemy!! On that note is there so many functions/code that can be used on and onsubmit event? How safe is javascript from hacking/injecting when using it for form validation etc or should i stick to php for the safer option for it and bugger the frilly js? Thanks for taking the time to read, any help would be great to help me master this form business!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.