Jump to content

Recommended Posts

I've included a file here that is a simple form for submitting data to my website.  As you can see onsubmit it should return the value of validate_form(this).  The validate part is not working.  Anyone can submit anything in this form, however I need that information to be validated.  If you have any ideas as to why it's not working, PLEASE post!  The smallest ideas make the biggest differences.  Thanks in advance for any of your help!

 

NE

 

<?
        // adduser.php
// 3 Oct 07

//@session_start();
include("header.php");
//checkLogin('0 1 2 3 4 5 6 7');

// if admin, advisor, president, treasurer, or an exec :: display page | else :: die?>

<script type="text/javascript">
	var lwr = 'abcdefghijklmnopqrstuvwxyz ';
	var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
	var num = '0123456789 ';

	function isvalid(parm, val){
		if(parm == "") return false;
		for(i=0; i<parm.length; i++){
			if(val.indexOf(parm.charAt(i),0) == -1) return false;
		}
		return true;
	}

	function validate_alpha(field, alerttxt){
		with(field){
			if(!isvalid(field.value, lwr+upr)){ 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_password_length(pass1, pass2, alerttxt){
		with(pass1, pass2){
			if(pass1.value.length < 6 || pass2.value.length < 6){ alert(alerttxt); return false }
			else{ return true }
		}
	}

	function validate_password(pass1, pass2, alerttxt){
		with(pass1, pass2){
			if(pass1 != pass2){ alert(alerttxt); return false }
			else{ return true }
		}
	}

	function validate_phone(field, alerttxt){
		with(phone){
			if(!isvalid(field.value, num){ alert(alerttxt); return false 
			else{ return true }
		}
	}

	function validate_complexes(complex, ucomplex, isadmin, alerttxt){
		with(complex, ucomplex, isadmin){
			alert(complex);
			alert(ucomplex);
			alert(isadmin);
			if(isadmin == 1){ return true; }
			else if(isadmin == 0){
				if(complex == ucomplex){ return true; }
				else if(complex != ucomplex){ alert(alerttxt); return false; }
				else{ alert(alerttxt); return false; }
			}else{ alert(alerttxt); return false; }
		}
	}

	function validate_form(thisform){
		with(thisform){
			if(validate_alpha(uname, "Not a valid input for name.")==false){ uname.focus(); return false }
			if(validate_email(email, "Not a valid e-mail address.")==false){ email.focus(); return false }
			if(validate_password_length(password1, password2, "Password must be at least 7 characters.")==false){ password1.focus(); return false }
			if(validate_password(password1, password2, "Passwords do not match.")==false){ password1.focus(); return false }
			if(validate_phone(phone, "Phone number is invalid.\nPlease use numbers and spaces only.")==false){ phone.focus(); return false }
			if(validate_alpha(green, "Please select your green.")==false){ green.focus(); return false }
			if(validate_complexes(complex, usercomplex, isadmin, "You do not have sufficient privileges to add a member to this complex.")==false){ complex.focus(); return false }
		}
	}
</script>

<div id="content2-pagetitle">Member Management</div>
<div id="content-title-noshade-size2">Add member</div>

<br>
<center>
<form name="adduser" action="doadduser.php" onsubmit="return validate_form(this)" method="post">
<input type="hidden" name="usercomplex" value="<? echo complex($_SESSION['user_id']); ?>">
<input type="hidden" name="isadmin" value="<? echo isadmin($_SESSION['user_id']) ?>">
<table>
	<tr>
		<td>Name </td>
		<td><input type="text" size="25"  name="uname"></td>
	</tr><tr>
		<td>Email </td>
		<td><input type="text" size="25"  name="email"></td>
	</tr>
	</tr><tr>
		<td>Password </td>
		<td><input type="password" size="25"  name="password1"></td>
	</tr>
	</tr><tr>
		<td>Re-enter Password </td>
		<td><input type="password" size="25"  name="password2"></td>
	</tr><tr>
		<td>Phone </td>
		<td><input type="text" size="25"  name="phone"></td>
	</tr><tr>
		<td>Green </td>
		<td>
			<select name="green">
				<option value=""></option>
				<option value="East">East</option>
				<option value="South">South</option>
				<option value="West">West</option>
			</select>
		</td>
	</tr><tr>
		<td>Complex </td>
		<td>
			<select name="complex">
				<option value=""></option>
			<?
				$comp = new Complex();
				$comps = $comp->getallcomplexes();
				if($comps){
					foreach($comps as $complex){?>
						<option value="<? echo $complex['complex'] ?>"><? echo $complex['complex'] ?></option>
					<?}
				}
			?>
			</select>
		</td>
	</tr>
</table><br>
<input type="submit" name="submit" value="Add member">
</form>
</center>
<?include("footer.php");?>

Link to comment
https://forums.phpfreaks.com/topic/72673-solved-javascript-help/
Share on other sites

I'm not exactly sure if this is what you're asking, but I would remove your "submit" button and just have a regular button or link that first performs your function checks and then the last line of your function can pass the submit value to the page for processing.

 

Something like this:

 

<html>
<head>
<script language="JavaScript">
<!--
function validate_form() { 
// Verifies that the first name was entered
if (document.my_form.first_name.value == "") {
alert("Please enter in a first name.");
document.my_form.first_name.focus();
return;
}
// Verifies that the last name was entered
if (document.my_form.last_name.value == "") {
alert("Please enter in a last name.");
document.my_form.last_name.focus();
return;
}
// Tells the submit what action to take
document.my_form.action='doadduser.php';
// Submits the form		
document.my_form.submit()
}
//-->

</script>
</head> 
<body>
<form>
<form name="my_form" action="" method="post" enctype="multipart/form-data">

ALL
YOUR
FORM
CODE
HERE

<table align="center" border="0" cellpadding="0" cellspacing="0" width="680">
<tr>
<td width="64" align="left"><a href="javascript:validate_form()" title="Save">Save</a></td>
</tr>
</table>
</form>
</body>
</html>

 

I personally like to put the checks within my function, instead of having multiple functions and then calling them all from within another function.

 

But, overall I think your problem might be that you are telling the form to submit (by using a submit button) before you are telling it to perform the checks...so, change the type from a submit to a regular button and have that button link(call) your form validation function.  Then have that function perform the submit for you once all checks are complete.

 

Hope that helps.

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.