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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.