0
我正在使用php mysql進行用戶註冊。我已經添加了javascript驗證來檢查是否有任何字段是空白的。但表單驗證不起作用。形式是Javascript驗證不能在html註冊表格中工作
<?php
include("header.php");
include ("includes/mainmenu.php");
?>
<!--Javasript Validation File Import -->
<script type="text/javascript" src="js/validation.js"></script>
<script type="text/javascript" src="js/formreg.js"></script>
<script type="text/javascript" language="JavaScript">
function HidePart(d) { document.getElementById(d).style.display = "none"; }
function ShowPart(d) { document.getElementById(d).style.display = "block"; }
function CheckboxChecked(b,d)
{
if(b) { ShowPart(d); }
else { HidePart(d); }
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<link rel="stylesheet" type="text/css" href="styles/styles.css" />-->
<link href="styles/form.css" rel="stylesheet" type="text/css">
<link href="styles/structure.css" rel="stylesheet">
<link href="styles/bootstrap.css" rel="stylesheet">
</head>
<body onLoad="document.registration.userid.focus();">
<br>
<div id="container" class="ltr">
<header id="header" class="info">
<h2>User Registration</h2>
</header>
<form action="insert.php" method="post" name="regForm" onSubmit="return valide()">
<ul>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Name</label>
<span><input id="fname" type="text" name="fname" class="field text1 fn" size="50"/></span><span>Please Enter Your full Name</span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Username</label>
<span><input id="Field0" type="text" name="usernamename" class="field text1 fn" size="50" /></span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="group">Educational Background</label></td>
<span><select id="Field0" class="field text1 fn" name="background" size="0">
<option selected="0" value="">select your Background</option>
<option value="Science">Science</option>
<option value="Humanities">Humanities</option>
<option value="Commerce">Commerce</option>
</select></span><span>Enter your HSC Background</span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Date of Birth</label>
<span><input id="Field0" type="date" name="birth" class="field text1 fn" size="50"/></span><span>Enter your date of Birth</span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Password</label>
<span><input id="Field0" type="password" name="password" class="field text1 fn" size="50" /></span><span>Enter your password</span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Password</label>
<span><input id="Field0" type="password" name="conpassword" class="field text1 fn" size="50" /></span><span>Enter your password again</span>
</li>
<li id="foli0" class="notranslate">
<label class="desc" id="title0" for="Field0">Email</label>
<span><input id="Field0" type="text" name="email" class="field text1 fn" size="50" /></span><span>Enter a valid email address</span>
</li>
<li class="buttons ">
<div>
<input id="submit" name="submit" class="btn" type="submit" value="Complete Registration"/></div>
</li>
</ul>
</form>
</div>
<?php
include("includes/footer.php");
?>
和驗證JavaScript代碼
function valide()
{
var fname=document.forms["regForm"]["fname"].value;
var username=document.forms["regForm"]["username"].value;
var subject = document.forms["regForm"]["background"].value;
var password=document.forms["regForm"]["password"].value;
var conpassword=document.forms["regForm"]["conpassword"].value;
var email=document.forms["regForm"]["email"].value;
var atpos=email.indexOf("@");
var dotpos=email.lastIndexOf(".");
if (fname==null || fname=="" || fname.length<7)
{
alert("Please provide Your full name");
return false;
}
else if (username==null || username=="")
{
if(username.length<6)
{
alert("Username must contains atleast 6 charecters");
}
else
alert("Please provide a username");
return false;
}
else if(subject==null || subject == "")
{
alert("Enter Background");
return false;
}
else if (password==null || password=="")
{
if(password.length<8)
{
alert("Password must contain atleast 8 charecters");
}
else
alert("Please insert your password");
return false;
}
else if (conpassword != password)
{
alert("Confirm password doesn't match");
return false;
}
else if (email==null || email=="")
{
alert("Email must be filled out.");
return false;
}
else if (atpos <1 || dotpos <atpos+2 || dotpos + 2 >= email.length)
{
alert("Not a valid e-mail address");
return false;
}
}
請幫助我。
所以有什麼實際問題?什麼不行? – FeRtoll 2013-04-06 20:45:18