peranha Posted November 20, 2007 Share Posted November 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 20, 2007 Share Posted November 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
peranha Posted November 20, 2007 Author Share Posted November 20, 2007 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. Quote Link to comment Share on other sites More sharing options...
pranav_kavi Posted November 20, 2007 Share Posted November 20, 2007 wich browser r u using??? Since it seems tat ur javascript is not running and ur code seems fine,try clearing the browser cache and then running the page again.Tis happens 2 me often in FF. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 21, 2007 Share Posted November 21, 2007 yeah - you might want to do as pranav_kavi said; because you script does just exactly like you want it to in FF v2.0.0.4 and IE6. I did not test it in IE7; as I do not have IE7 on this particular computer. Quote Link to comment Share on other sites More sharing options...
peranha Posted November 26, 2007 Author Share Posted November 26, 2007 Yes I did this, and it now works. I did not test it in IE either as i do not use IE. Thanks for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.