我希望在插入mongodb之前验证输入字段有一些帮助。我在html5中创建一个注册表单,并使用php将值从from插入mongodb。我想验证字段的身份编号,南非身份数字长度为13位,我想将其分解以从身份编号中获取性别。我创建了一个javascript文件,用于验证字段的长度,但在javascript文件测试输入长度后仍然继续。我html5表单下面是它的css和链接的javascript文件。以下是注册表格registration form below register.php
<html>
<title id="top_title">
</title>
<div id="wrapper">
<header id="top_header">
</style>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width , initial-scale=1.0">
<link rel="stylesheet" href="styles/register.css"/>
<script type="text/javascript" src="validateReg.js" language="javascript"></script>
<div id="title_image"><marquee behavior="alternate"><h1>Welcome To Tshwane Metro Licensing System</h1></marquee>
<img src="MetroPoliceLogo.jpg" alt="not found" id="logo" width="100" height="100">
</header>
<body id="main_body">
<section id="main_section">
<form action="process_register.php" method="post" id="id_reg">
<table id="login_box">
<tr><td><h1 id="login_header">Register</h1></td></tr>
<tr><td>Username</td><td><input type="email" required="true" id="reg_username" name="reg_username"></td></tr>
<tr><td>Password</td><td><input type="password" id="reg_password" required="true" name="reg_password"></td></tr>
<tr><td>Name</td><td><input type="text" id="reg_name" required="true" name="reg_name"></td></tr>
<tr><td>Surname</td><td><input type="text" id="reg_surname" required="true" name="reg_surname"></td></tr>
<tr><td>Identity Number</td><td><input type="number" id="reg_idNumber" required="true" name="reg_idNumber" minlength="13" maxlength="13"></td></tr>
<tr><td>Gender</td><td><input type="text" id="reg_gender" required="true" name="reg_gender"></td></tr>
<tr><td>Race</td><td><input type="text" id="reg_race" required="true" name="reg_race"></td></tr>
<tr><td>Nationality</td><td><input type="text" id="reg_nationality" required="true" name="reg_nationality"></td></tr>
<tr><td>Address</td><td><input type="text" id="reg_address" required="true" name="reg_address"></td></tr>
<tr><td><input type="submit" id="btnReg" value="Register" name="btnReg" onclick="validateID()"></td></tr>
</table>
</form>
<footer>
</footer>
</section>
<aside id="side_news">
<img src="signup.jpg" width="150" height="140" >
</aside>
<footer id="main_footer">
<p>Copyright Tshwane Metro Licensing 2016 </p>
</footer>
</body>
</div>
</html>
在数据库中插入值的php脚本如下所示 process_register.php
<?php
if(isset($_POST['btnReg']))
{
$connection = new Mongo();
$database = $connection->selectDB('licenseDiskDB');
$collection = $database->selectCollection('tblusers');
/*$userQuery = array('reg_username' =>$_POST['reg_username'],
'reg_idNumber' =>$_POST['reg_idNumber']);
$cursor = $collection->findOne($userQuery);
if($cursor['reg_username']!=$_POST['reg_username'])
{*/
$userQuery = array('reg_username' =>$_POST['reg_username']);
$cursor = $collection->findOne($userQuery);
$userQueryID = array('reg_idNumber' =>$_POST['reg_idNumber']);
$IDUser = $collection->findOne($userQueryID);
if(($cursor)||($IDUser))
{
echo 'Already Registered!';
}else
{
try
{
$newUser = array();
$newUser['reg_username']=$_POST['reg_username'];
$newUser['reg_password']=$_POST['reg_password'];
$newUser['reg_name']=$_POST['reg_name'];
$newUser['reg_surname']=$_POST['reg_surname'];
$newUser['reg_idNumber']=$_POST['reg_idNumber'];
$newUser['reg_gender']=$_POST['reg_gender'];
$newUser['reg_race']=$_POST['reg_race'];
$newUser['reg_nationality']=$_POST['reg_nationality'];
$newUser['reg_address']=$_POST['reg_address'];
$collection->insert($newUser);
header("Location:http://localhost:8080/phpproject2/reg_success.php");
} catch (MongoCursorException $ex)
{
die('Insert Failed'.$ex->getMessage());
}
}
/* }else
{
echo 'Insert Not Comleted';
}*/
}
?>
测试输入长度为13位的javascript低于
function validateID()
{
var idNumber = document.getElementById('reg_idNumber');
if((isNaN(idNumber))===true)
{
if(idNumber.toString().length===13)
{
alert('Soputh African ID');
}else
{
alert('Incorrect Identity Number length');
window.location="http://localhost:8080/PhpProject/register.php";
}
}else
{
alert('Identity Number should not contain characters');
return true;
}
}