Jump to content

[SOLVED] check form


peranha

Recommended Posts

This is the functions.js page

 

function checkForm() {
        var username = document.register.username;
        var password = document.register.password;
	var firstname = document.register.firstname;
	var lastname = document.register.lastname;
	var address = document.register.address;
	var city = document.register.city;
	var state = document.register.state;
	var ZIP = document.register.ZIP;
	var Email = document.register.Email;
	var phone = document.register.phone;

//Check to make sure posted lengths are sensible

        if (username.value.length < 1) 
	{
        alert("Enter a User Name.");
        return false;
        }
        else 
		{ 
		if (password.value.length < 1) 
		{
            alert("Enter a Password.");
            return false;
		}
		else 
			{ 
			if (firstname.value.length < 1) 
			{
                alert("Enter Your First Name.");
                return false;
                }
			else 
				{
				if (lastname.value.length < 1)
				{
				alert("Enter Your First Name.");
				return false;
				}
				else
					{
					if (address.value.length < 1)
					{
					alert("Enter Your Address.");
					return false;
					}
					else
						{
						if (city.value.length < 1)
						{
						alert("Enter The City You Live In.");
						return false;
						}
						else
							{
							if (state.value.length < 1)
							{
							alert("Enter The State You Live In.");
							return false;
							}
							else
								{
								if (state.value.length > 2)
								{
								alert("Use The States 2 letter abbreviation.");
								return false;
								}
								else
									{
									if (ZIP.value.length < 1)
									{
									alert("Enter Your 5 Digit ZIP code.");
									return false;
									}
									else
										{
										if (Email.value.length < 1)
										{
										alert("Enter Your Email Address.");
										return false;
										}
										else
											{
											if (phone.value.length < 1)
											{
											alert("Enter Your Phone Number.");
											return false;
											}				
											else 
												{
												return true;								
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
}

 

Here is the register page.

 

<html>
<head>
<title>Registration Page</title>

<script src="includes/functions.js"></script>

<basefont face="Arial">

</head>
<BODY>

<form action="register1.php" method="post" name="register" onsubmit="return checkForm(this)">
<TABLE id="register" cellpadding="1" border="1" bgcolor="lightblue">
<TR>
	<TD>Username:</TD><TD><input type="text" name="username"></TD>
</TR>
<TR>
	<TD>Password:</TD><TD><input type="password" name="password"></TD>
</TR>
<TR>
	<TD>First Name:</TD><TD><input type="text" name="firstname"></TD>
</TR>
<TR>
	<TD>Last Name:</TD><TD><input type="text" name="lastname"></TD>
</TR>
<TR>
	<TD>Address:</TD><TD><input type="text" name="address"></TD>
</TR>
<TR>
	<TD>City:</TD><TD><input type="text" name="city"></TD>
</TR>
<TR>
	<TD>State:</TD><TD><input type="text" name="state"></TD>
</TR>
<TR>
	<TD>ZIP Code:</TD><TD><input type="text" name="ZIP" value = "12345"></TD>
</TR>
<TR>
	<TD>Email:</TD><TD><input type="text" name="Email"></TD>
</TR>
<TR>
	<TD>Phone Number:</TD><TD><input type="text" name="phone"></TD>
</TR>
<TR>
	<TD><input type="submit" name="submit" value="Submit"></TD>
</TR>
</TABLE>
</form>

<H5>

 

I wrote this to check and make sure items are filled in, but it does not work.  it is supposed to check the register page, and give a popup if one of the items is not filled in.

 

The register1.php page adds the data to the database.

 

If anyone could help that would be great.  I am new to JS, so this is new to me.

 

Thanks in advance.

Link to comment
Share on other sites

peranha, I tested your code and it seems to do what you want it to do. The javascript you wrote validates every field in the form before it will allow the form action to proceed to the register1.php page.

 

On a side note; I hope that you are also validating with PHP and not just JavaScript by itself. It good to use client side validation, but it is always better to use server side validation and if your using them both, that's probably the best method of form validation.

Link to comment
Share on other sites

On a side note; I hope that you are also validating with PHP and not just JavaScript by itself. It good to use client side validation, but it is always better to use server side validation and if your using them both, that's probably the best method of form validation.

 

Yes I am using PHP to validate as well, but the javascript does not validate before, it just sends it, and the php will validate.

 

Here is the php validate code that I have

 

// open connection
$connection = mysql_connect($server, $user, $pass) or die ("Unable to connect!");

// get form input
// check to make sure it's all there
// escape input values for greater safety
$username = empty($_POST['username']) ? die ("Enter A User Name") : mysql_escape_string($_POST['username']);
$password = empty($_POST['password']) ? die ("Enter A Password") : mysql_escape_string($_POST['password']);
$firstname = empty($_POST['firstname']) ? die ("Enter name") : mysql_escape_string($_POST['firstname']);
$lastname = empty($_POST['lastname']) ? die ("Enter name") : mysql_escape_string($_POST['lastname']);
$address = empty($_POST['address']) ? die ("Enter Address 1") : mysql_escape_string($_POST['address']);
$city = empty($_POST['city']) ? die ("Enter City") : mysql_escape_string($_POST['city']);
$state = empty($_POST['state']) ? die ("Enter State Abbreviation") : mysql_escape_string($_POST['state']);
$ZIP = empty($_POST['ZIP']) ? die ("Enter Zip Code") : mysql_escape_string($_POST['ZIP']);
$Email = empty($_POST['Email']) ? die ("Enter Email") : mysql_escape_string($_POST['Email']);
$phone = empty($_POST['phone']) ? die ("Enter Phone Number") : mysql_escape_string($_POST['phone']);

 

I just want it to give the popup after submitted, not actually go to the page and give the php error.

 

Is there anything that has to be put on this page for the js to run first?

 

Thanks.

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.