2 Replies - 1312 Views - Last Post: 06 May 2010 - 03:41 PM

#1 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

form validation

Posted 06 May 2010 - 02:09 PM

Hi. I am trying to do some sim[ple form validation using javascript. I have a simple html file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send Card</title>
<script language="Javascript" src="gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>
<form action="getName.php" onsubmit="return validate_form(this);">
Receiver (To): <input type="text" name="name"><br>
Sender (From): <input type="text" name="sender"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="Submit">
</form>
</body>

</html>


Which calls up my javascript and php file (The php file should only be used when the javascript has found no errors).

Now I have a js file which does some validation

<script type="text/javascript">

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_form(thisform) {
	with (thisform) {
		if (validate_required(name,"Please specify your name!")==false) {
			name.focus();
			return false;
		}
		if (validate_required(sender,"Please specify the receivers name!")==false) {
			sender.focus();
			return false;
		}
		if (validate_required(email,"Email must be filled out!")==false) {
			email.focus();
			return false;
		}
		if (validate_email(email,"Not a valid e-mail address!")==false) {
			email.focus();
			return false;
		}
	}
}
</script>


When I run it on my server, I get the form and enter nothing into it, I just click submit. However, it goes straight to the php file and brings up my flash movie, which it shouldnt do. When nothing is entered, the js file should return the error. I thought I had everything set up correctly, but obviously not. Can someone see where I am going wrong?

cheers

Is This A Good Question/Topic? 0
  • +

Replies To: form validation

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: form validation

Posted 06 May 2010 - 02:52 PM

Hey, let me see if I can help you out. I'm debugging your code and I'll write down any problems I see:
1. Indent your html
2. On loading the JS file, I get an error on line 1. You left the <script> tag in, which is wrong. .js files are pure Javascript and no HTML.
3. You should stay away from with statements.
4. You should put the var keyword on front of the variables apos and dotpos so they will not be global.
5. The exclamation marks in the alerts were overkill.
6. Corrected "Please specify the receiver's name" to "Please specify the sender's name"

And that's all the problems I had with it. Here's the modified code:

function validate_required(field, alerttxt) {
	if (field.value == null || field.value == "") {
		alert(alerttxt);
		return false;
	}
	else {
		return true;
	}
}

function validate_email(field, alerttxt) {
	var apos = field.value.indexOf("@")
	var dotpos = field.value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) {
		alert(alerttxt);
		return false;
	} else {
		return true;
	}
}

function validate_form(thisform) {
	if (validate_required(thisform.name,"Please specify your name.")==false) {
		thisform.name.focus();
		return false;
	}
	if (validate_required(thisform.sender,"Please specify the sender's name.")==false) {
		thisform.sender.focus();
		return false;
	}
	if (validate_required(thisform.email,"Email must be filled out.")==false) {
		thisform.email.focus();
		return false;
	}
	if (validate_email(thisform.email,"Not a valid e-mail address.")==false) {
		thisform.email.focus();
		return false;
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: form validation

Posted 06 May 2010 - 03:41 PM

Thanks. Made your changes and now the validation works great. Very new with web technologies so bare with me :stupid:

So now the validation works, the php file does not seem to work. My html file is in my first post. What should happen is the validation, and if this is successful, it should send the data to my php. My php is
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" align="middle">
	<param name="allowScriptAccess" value="sameDomain" />
	<param name="movie" value="card.swf" />
	<param name="quality" value="high" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="flashvars" value="myVar=<?php echo $_POST['name'] ?> & myVar1=<?php echo $_POST['sender'] ?>" />
	<embed src="card.swf" FlashVars="myVar=<?php echo $_POST['name'] ?> & myVar1=<?php echo $_POST['sender'] ?>" quality="high" bgcolor="#FFFFFF" width="550" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>



Now, the strange thing is the movie is displaying when the validation is correct. However, the php echos which send data to my movie is not working. The movie should display the senders name and receivers name. I know the above is ok because it was working fine before. It has only stopped working since I have made changes to my html file. Have I done something which is stopping the data from going through?

cheers
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1