Jump to content

Simple Form Validation


Jonathan Harvey
 Share

Recommended Posts

Below I have a simple Javascript form validation. I know that it's not very complicated and won't check the password but it's just to practice javascript.

 <!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>Registration</title></head><body><script type="text/javascript">//lets validate the form, note it didn't work before because I called a function before I defined it.//lets alert the user and return and call whats_missing, so we can return a value to the formfunction alertuser(missing, missingalert) {	alert("please fix " + missingalert);	return false;	//function to check whats missing, or if email be screwed upfunction whats_missing(){	if(document.register.username.value == null||"") {		missing = username;		alertuser(missing, "Please fix " + missing)		return false;	}	else if(document.register.password.value == null||"")(		missing = password		alertuser(missing, "Please fix " + missing)		return false;}	else {		return true;	}					}</script><form name="register" action="thanksforregistering.html" method="post" onSubmit="return whats_missing()"><input type="text" name="username" size="20"  /><input type="password" name="password" size="20"  /><input type="submit" value="Sign up"  /></form></body></html>

When I run this code with no username nothing happens. can somebody tell me what I'm doing wrong? Thanks!

Edited by Jonathan harvey
Link to comment
Share on other sites

  1. You have forgotten to close this function, therefore, all the functions that follow it won't exist because they're considered to be inside this one and this one returns on the second line.
  2. You're not doing anything with the "missing" parameter

function alertuser(missing, missingalert) {alert("please fix " + missingalert);return false;//function to check whats missing, or if email be screwed upfunction whats_missing(){

Link to comment
Share on other sites

  1. You have forgotten to close this function, therefore, all the functions that follow it won't exist because they're considered to be inside this one and this one returns on the second line.
  2. You're not doing anything with the "missing" parameter

function alertuser(missing, missingalert) {alert("please fix " + missingalert);return false;//function to check whats missing, or if email be screwed upfunction whats_missing(){

I fixed the missing parameter right after this I noticed it, but I guess I didn't notice I forget to close the function. Thanks for the help!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...