Hi, I am having some problems with a project I am working on. I cannot get Java to validate my php forms. When I press the submit button on the form the java validation and the php validation is skipped and the data is submitted leaving blank entries in my db. This should not be happening as the java should validate the form and if java is disabled for whatever reason the php should then kick in.
I cant see where im going wrong, so any help would be really appreciated.
Code for page listed below (slightly mangled as had to cut some out). Thanks!
<!-- Javascript to validate user details form -->
<script language="JavaScript">
function checkForm()
{
var cUserName, cPass1, cPass2, cFirstName, cLastName, cEmail, cAddr1, cAddr2, cAddr2, cAddr3, cTown, cCounty, cPostCode, cHomeTel, cMob, cWebsite, csubject, cmessage;
with(window.document.msgform)
{
cUserName = txtUserName;
cPass1 = txtPassword1;
cPass2 = txtPassword2;
cFirstName = txtFirstName;
cLastName = txtLastName;
cEmail = txtEmail;
cAddr1 = txtAddressLine1;
cAddr2 = txtAddressLine2;
cAddr3 = txtAddressLine3;
cTown = txtTown;
cCounty = txtCounty;
cPostCode = txtPostCode;
cHomeTel = txtHomeTel;
cMob = txtMobile;
cWebsite = txtWebsite;
}
if(trim(cUserName.value) == '')
{
alert('Please enter a UserName');
cUserName.focus();
return false;
}
else if(trim(cPass1.value) == '')
{
alert('Please enter a password');
cPass1.focus();
return false;
}
else if(trim(cPass2.value) == '')
{
alert('Please re-enter your chosen password');
cPass2.focus();
return false;
}
else if(!trim(cPass2.value) == trim(cPass1.value))
{
alert('Please ensure your password is the same in both password fields');
cPass1.focus();
return false;
}
else if(trim(cFirstName.value) == '')
{
alert('Please enter your first name');
cFirstName.focus();
return false;
}
else if(trim(cEmail.value) == '')
{
alert('Please enter your Email address');
cEmail.focus();
return false;
}
else if(!isEmail(trim(cEmail.value)))
{
alert('Your Email address is not valid');
cEmail.focus();
return false;
}
else
{
cUserName.value = trim(cUserName.value);
cPass2.value = trim(cPass2.value);
cFirstName.value = trim(cFirstName.value);
cLastName.value = trim(cLastName.value);
cEmail.value = trim(cEmail.value);
cAddr1.value = trim(cAddr1.value);
cAddr2.value = trim(cAddr2.value);
cAddr3.value = trim(cAddr3.value);
cTown.value = trim(cTown.value);
cCounty.value = trim(cCounty.value);
cPostCode.value = trim(cPostCode.value);
cHomeTel.value = trim(cHomeTel.value);
cMob.value = trim(cMob.value);
cWebsite.value = trim(cWebsite.value);
return true;
}
}
function trim(str)
{
return str.replace(/^\s+|\s+$/g,'');
}
function isEmail(str)
{
var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
return regex.test(str);
}
</script>
</head>
<body>
<!-- PHP to validate user details form -->
<?php
$errmsg = '';
$txtUserName = '';
$txtPassword1 = '';
$txtPassword2 = '';
$txtFirstName = '';
$txtEmail = '';
if(isset($_POST['btnRegister']))
{
$txtUserName = $_POST['txtUserName'];
$txtPassword1 = $_POST['txtPassword1'];
$txtPassword2 = $_POST['txtPassword2'];
$txtFirstName = $_POST['txtFirstName'];
$txtEmail = $_POST['txtEmail'];
if(trim($txtUserName) == '')
{
$errmsg = 'Please enter a username';
}
else if(trim($Password1) == '')
{
$errmsg = 'Please enter a password';
}
else if(trim($Password2) == '')
{
$errmsg = 'Please re-enter your password';
}
else if(!trim($Password1) == trim($Password2))
{
$errmsg = 'Please ensure your passwords match';
}
else if(trim($txtFirstName) == '')
{
$errmsg = 'Please enter your first name';
}
else if(!isEmail($txtEmail))
{
$errmsg = 'Your email address is not valid';
}
}
?>
</div>
<?php
if(isset($_POST['btnRegister']) || $errmsg != '')
{
include 'config.php';
include 'opendb.php';
$txtUserName = $_POST['txtUserName'];
$txtPassword1 = $_POST['txtPassword1'];
$txtFirstName = $_POST['txtFirstName'];
$txtLastName = $_POST['txtLastName'];
$txtEmail = $_POST['txtEmail'];
$txtAddressLine1 = $_POST['txtAddressLine1'];
$txtAddressLine2 = $_POST['txtAddressLine2'];
$txtAddressLine3 = $_POST['txtAddressLine3'];
$txtTown = $_POST['txtTown'];
$txtCounty = $_POST['txtCounty'];
$txtPostCode = $_POST['txtPostCode'];
$txtHomeTel = $_POST['txtHomeTel'];
$txtMobile = $_POST['txtMobile'];
$txtWebsite = $_POST['txtWebsite'];
$query = "INSERT INTO reguser (userName, password, firstName, lastName, email, addressLine1, addressLine2, addressLine3, town, county, postCode, homeTel, mobileTel, website, date) VALUES ('$txtUserName', PASSWORD('$txtPassword1'), '$txtFirstName', '$txtLastName', '$txtEmail', '$txtAddressLine1', '$txtAddressLine2', '$txtAddressLine3', '$txtTown', '$txtCounty', '$txtPostCode', '$txtHomeTel', '$txtMobile', '$txtWebsite', NOW())";
mysql_query($query) or die('Error, insert query failed');
$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
include 'closedb.php';
echo "<p class ='text2'>Your user details have been added.</p>";
}
else
{
?>
<form method="post" name="frmRegister" id="frmRegister">
<table width="300" align="left" cellspacing = "7">
<tr>
<td width="150"><p class="text3">User Name*</p></td>
<td><input name="txtUserName" type="text" id="txtUserName"></td>
</tr>
<tr>
<td width="150"><p class="text3">Password*</p></td>
<td><input name="txtPassword1" type="password" id="txtPassword1"></td>
</tr>
<tr>
<td width="150"><p class="text3">Re-Enter Password*</p></td>
<td><input name="txtPassword2" type="password" id="txtPassword2"></td>
</tr>
<tr>
<td width="150"><p class="text3">First Name*</p></td>
<td><input name="txtFirstName" type="text" id="txtFirstName"></td>
</tr>
<tr>
<td width="150"><p class="text3">Last Name</p></td>
<td><input name="txtLastName" type="text" id="txtLastName"></td>
</tr>
<tr>
<td width="150"><p class="text3">Email*</p></td>
<td><input name="txtEmail" type="text" id="txtEmail"></td>
</tr>
<tr>
<td width="150"><p class="text3">Address Line1</p></td>
<td><input name="txtAddressLine1" type="text" id="txtAddressLine1"></td>
</tr>
<tr>
<td width="150"><p class="text3">Address Line2</p></td>
<td><input name="txtAddressLine2" type="text" id="txtAddressLine2"></td>
</tr>
<tr>
<td width="150"><p class="text3">Address Line3</p></td>
<td><input name="txtAddressLine3" type="text" id="txtAddressLine3"></td>
</tr>
<tr>
<td width="150"><p class="text3">Town</p></td>
<td><input name="txtTown" type="text" id="txtTown"></td>
</tr>
<tr>
<td width="150"><p class="text3">County</p></td>
<td><input name="txtCounty" type="text" id="txtCounty"></td>
</tr>
<tr>
<td width="150"><p class="text3">PostCode</p></td>
<td><input name="txtPostCode" type="text" id="txtPostCode"></td>
</tr>
<tr>
<td width="150"><p class="text3">Home Tel</p></td>
<td><input name="txtHomeTel" type="text" id="txtHomeTel"></td>
</tr>
<tr>
<td width="150"><p class="text3">Mobile</p></td>
<td><input name="txtMobile" type="text" id="txtMobile"></td>
</tr>
<tr>
<td width="150"><p class="text3">Website</p></td>
<td><input name="txtWebsite" type="text" id="txtWebsite"></td>
</tr>
<tr>
<td width="150"> </td>
<td><input type="submit" id="btnRegister" name="btnRegister" value="Register" onclick="return checkForm();"></td>
</tr>
</table>
</form>
<p class ="text2">Fields with a * are mandatory</p>
<?php
}
?>
</div>
</body>
</html>