Jump to content

Javascript submission/form questions.


UrbanDweller

Recommended Posts

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  :confused:

  • 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!!

 

Link to comment
https://forums.phpfreaks.com/topic/255446-javascript-submissionform-questions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.