我的网站注册有问题。
如果我填写用户名字段,并将其他字段留空,则不会显示错误警报,只会将用户名保存到数据库中。应该进行验证,比如检查年龄是否低于18,但我无法确定错误的位置。
register.php(此处为javascript代码和php代码):
<html>
<center><h1>Register Below</h1></center>
<body>
<br/>
<script>
function getText()
{
boolean sub = true;
var name = document.getElementById("t1").value;
if(name.length < 8 || name.length > 50)
{
sub=false;
alert("Invalid Username, must be 8 - 10 characters!")
document.getElementById("sample").innerHTML="*";
}
else
{
document.getElementById("sample").innerHTML = name;
}
var pass = document.getElementById("t2").value;
if(pass.length < 8 || pass.length > 50)
{
sub=false;
alert("Invalid Password, must be 8 - 10 characters!")
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var rpass = document.getElementById("t3").value;
if(rpass!=pass)
{
sub=false;
alert("Password dont match!")
document.getElementById("sample3").innerHTML="*";
}
else
{
sub=true;
}
var fname = document.getElementById("t4").value;
if(fname.length<5||fname.length>15)
{
sub=false;
alert("Invalid firstname, must be 5 - 15 characters!")
}
else
{
document.getElementById("sample11").innerHTML = fname;
}
var lname = document.getElementById("t5").value;
if(lname.length<5||lname.length>15)
{
sub=false;
alert("Invalid lastname, must be 5 - 15 characters!")
}
else
{
document.getElementById("sample12").innerHTML = lname;
}
int age = parseInt(document.getElementById("t2").value);
if(age < 18)
{
sub=false;
alert("Minors not allowed.")
}
else
{
document.getElementById("sample4").innerHTML = age;
}
var email = document.getElementById("t7").value;
if(email.length < 8 || email.length > 50)
{
sub=false;
alert("Invalid Email, must be 8 - 50 characters!")
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var lotnum = document.getElementById("t8a").value;
if(lotnum.length<3||lotnum.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample13").innerHTML = lotnum;
}
var sub = document.getElementById("t8b").value;
if(sub.length<5||sub.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample14").innerHTML = sub;
}
var city = document.getElementById("t8c").value;
if(city.length<5||city.length>10)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample15").innerHTML = city;
}
var country = document.getElementById("t9").value;
if(country.length<3||country.length>30)
{
sub=false;
alert("Location Invalid.")
}
else
{
document.getElementById("sample16").innerHTML = country;
}
int area = parseInt(document.getElementById("t10a").value);
if(area.length<3||area.length>8)
{
sub=false;
alert("Invalid Area Code, must be 3 - 8 digits!")
}
else
{
document.getElementById("sample16").innerHTML = area;
}
int telnum = parseInt(document.getElementById("t10b").value);
if(telnum.length<10||telnum.length>12)
{
sub=false;
alert("Invalid Number!")
}
else
{
document.getElementById("sample16").innerHTML = telnum;
}
if(sub==false)
{
return false;
}
}
</script>
<center>
<form method = "post" action = "insert.php" onSubmit="return getText()">
<table>
<tr>
<td>Username:</td>
<td colspan="3"><input name="t1" type="text" id="t1"><span id="sample">
</span><br></td>
</tr>
<tr>
<td>Password:</td>
<td colspan="3"><input name="t2" type="password" id="t2"><span id="sample2">
</span><br></td></td>
</tr>
<tr>
<td>Re-type Password:</td>
<td colspan="3"><input name="t3" type="password" id="t3"><span id="sample3">
</span><br></td></td>
</tr>
<tr>
<td>Firstname:</td>
<td colspan="3"><input name="t4" type="text" id="t4"><br></td>
</tr>
<tr>
<td>Lastname:</td>
<td colspan="3"><input name="t5" type="text" id="t5"><br></td>
</tr>
<tr>
<td>Age:</td>
<td colspan="3"><input name="t6" type="text" id="t6"><br></td>
</tr>
<tr>
<td>Email:</td>
<td colspan="3"><input name="t7" type="email" id="t7"><br></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="t8a" id="t8a"><br></td>
<td><input type="text" name="t8b" id="t8b"><br></td>
<td><input type="text" name="t8c" id="t8c"><br></td>
</tr>
<tr>
<td></td>
<td>Lot/Block#</td>
<td>Brgy/Subd</td>
<td>City/Municipality</td>
</tr>
<tr>
<td>Country:</td>
<td colspan="3"><input name="t9" type="text" id="t9"><br></td>
</tr>
<tr>
<td>Contact#:</td>
<td><input name="t10a" type="text" id="t10a"><br></td>
<td><input name="t10b" type="text" id="t10b"><br></td>
</tr>
<tr>
<td></td>
<td>Area Code</td>
<td>Number<td>
</tr>
</table>
<br><br>
<button type="submit" onClick="getText()">Submit</button><br><br>
<p id="log">
</p>
</form>
</center>
</body>
以及我的数据库的代码:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$db = mysql_connect("localhost","root","");
if($db){
mysql_select_db("user1",$db);
$username = $_POST['t1'];
$password = $_POST['t3'];
$firstname = $_POST['t4'];
$lastname = $_POST['t5'];
$age = $_POST['t6'];
$email = $_POST['t7'];
$lot = $_POST['t8a'];
$sub = $_POST['t8b'];
$city = $_POST['t8c'];
$country = $_POST['t9'];
$area = $_POST['t10a'];
$num = $_POST['t10b'];
if(mysql_query("insert into regform(username,password,firstname,lastname,age,email,lot,sub,city,country,area,num) values('$username','$password','$firstname','$lastname','$age','$email','$lot','$sub','$city','$country','$area','$num')"))
{
echo "Register Successful. Click <a href='index.html'> Here </a> to return";
}
else
{
echo "Registration Failed".mysql_error();
}
}
else{
echo "Cannot Connect in Database!";
}
?>
答案 0 :(得分:0)
试试这段代码
<html>
<center><h1>Register Below</h1></center>
<body>
<br/>
<script>
function getText()
{
var sub = true ;
var name = document.getElementById("t1").value;
if(name.length < 8 || name.length > 50)
{
sub=false;
alert("Invalid Username, must be 8 - 10 characters!");
document.getElementById("sample").innerHTML="*";
}
else
{
document.getElementById("sample").innerHTML = name;
}
var pass = document.getElementById("t2").value;
if(pass.length < 8 || pass.length > 50)
{
sub=false;
alert("Invalid Password, must be 8 - 10 characters!");
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var rpass = document.getElementById("t3").value;
if(rpass !== pass)
{
sub=false;
alert("Password dont match!");
document.getElementById("sample3").innerHTML="*";
}
else
{
sub=true;
}
var fname = document.getElementById("t4").value;
if(fname.length<5||fname.length>15)
{
sub=false;
alert("Invalid firstname, must be 5 - 15 characters!");
}
else
{
document.getElementById("sample11").innerHTML = fname;
}
var lname = document.getElementById("t5").value;
if(lname.length<5||lname.length>15)
{
sub=false;
alert("Invalid lastname, must be 5 - 15 characters!");
}
else
{
document.getElementById("sample12").innerHTML = lname;
}
var age = parseInt(document.getElementById("t2").value);
if(age < 18)
{
sub=false;
alert("Minors not allowed.");
}
else
{
document.getElementById("sample4").innerHTML = age;
}
var email = document.getElementById("t7").value;
if(email.length < 8 || email.length > 50)
{
sub=false;
alert("Invalid Email, must be 8 - 50 characters!");
document.getElementById("sample2").innerHTML="*";
}
else
{
sub=true;
}
var lotnum = document.getElementById("t8a").value;
if(lotnum.length<3||lotnum.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample13").innerHTML = lotnum;
}
var sub = document.getElementById("t8b").value;
if(sub.length<5||sub.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample14").innerHTML = sub;
}
var city = document.getElementById("t8c").value;
if(city.length<5||city.length>10)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample15").innerHTML = city;
}
var country = document.getElementById("t9").value;
if(country.length<3||country.length>30)
{
sub=false;
alert("Location Invalid.");
}
else
{
document.getElementById("sample16").innerHTML = country;
}
var area = parseInt(document.getElementById("t10a").value);
if(area.length<3||area.length>8)
{
sub=false;
alert("Invalid Area Code, must be 3 - 8 digits!")
}
else
{
document.getElementById("sample16").innerHTML = area;
}
var telnum = parseInt(document.getElementById("t10b").value);
if(telnum.length<10||telnum.length>12)
{
sub=false;
alert("Invalid Number!");
}
else
{
document.getElementById("sample16").innerHTML = telnum;
}
if(sub === false)
{
return false;
}
}
</script>
<center>
<form method = "post" action = "insert.php">
<table>
<tr>
<td>Username:</td>
<td colspan="3"><input name="t1" type="text" id="t1"><span id="sample">
</span><br></td>
</tr>
<tr>
<td>Password:</td>
<td colspan="3"><input name="t2" type="password" id="t2"><span id="sample2">
</span><br></td></td>
</tr>
<tr>
<td>Re-type Password:</td>
<td colspan="3"><input name="t3" type="password" id="t3"><span id="sample3">
</span><br></td></td>
</tr>
<tr>
<td>Firstname:</td>
<td colspan="3"><input name="t4" type="text" id="t4"><br></td>
</tr>
<tr>
<td>Lastname:</td>
<td colspan="3"><input name="t5" type="text" id="t5"><br></td>
</tr>
<tr>
<td>Age:</td>
<td colspan="3"><input name="t6" type="text" id="t6"><br></td>
</tr>
<tr>
<td>Email:</td>
<td colspan="3"><input name="t7" type="email" id="t7"><br></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="t8a" id="t8a"><br></td>
<td><input type="text" name="t8b" id="t8b"><br></td>
<td><input type="text" name="t8c" id="t8c"><br></td>
</tr>
<tr>
<td></td>
<td>Lot/Block#</td>
<td>Brgy/Subd</td>
<td>City/Municipality</td>
</tr>
<tr>
<td>Country:</td>
<td colspan="3"><input name="t9" type="text" id="t9"><br></td>
</tr>
<tr>
<td>Contact#:</td>
<td><input name="t10a" type="text" id="t10a"><br></td>
<td><input name="t10b" type="text" id="t10b"><br></td>
</tr>
<tr>
<td></td>
<td>Area Code</td>
<td>Number<td>
</tr>
</table>
<br><br>
<button type="submit" onClick="getText()">Submit</button><br><br>
<p id="log">
</p>
</form>
</center>
</body>