PHP验证用户注册信息是否合法
关键函数
通过JavaScript对用户注册信息的合法性进行验证时,经常需要对数值型数据进行检验。
JavaScript可以应用isNaN()函数实现上述功能,下面对该函数进行讲解。
isNaN()函数
用于判断该函数的参数是否为数值型数据,是返回False,否则返回True。
语法
bool isNaN(var)
参数var为要进行判断的数据。
功能实现
用户填写完注册信息后,为了保证这些信息的合法性,还应该对这些信息进行验证。对注册信息的合法性进行验证既可以应用PHP代码实现,也可以应用JavaScript实现。开发人员可以根据实际情况进行选取,如果担心客户端浏览器对JavaScript进行了限制,则可以采用PHP代码实现,但使用这种方法对注册信息进行验证是在服务器中进行的,所以会浪费大量的服务器资源。而应用JavaScript脚本对用户的注册信息进行验证,是客户端浏览器实现的,所以会在很大程度上节省服务器资源。本站对用户注册信息的验证通过JavaScript实现。在编写用户注册信息验证模块时,将实现用户注册信息验证的代码封装到自定义函数chkinput login()中,并在表单《form》标记的onSubmit事件中对该函数进行调用,Chkinput login()函数的代码如下。
以下为
以下为<?php
include_once("top.php");
?>
<table width="780" height="370" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="175" valign="top"><table width="175" height="120" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D2D7DD">
<tr>
<td bgcolor="#F2F3F5" valign="top">
<?php
include_once("left.php");
?>
</td>
<td width="10"> </td>
<td width="595" valign="top"><table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><img src="www.zw361.com/images/reg.gif" width="590" height="35"></td>
</tr>
</table>
<table width="500" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td> </td>
</tr>
</table>
<table width="500" height="300" border="0" align="center" cellpadding="0" cellspacing="0">
<script language="javascript">
function chkinput_login(form){ //断用户是否输入了用户名
if(form.usernc.value==""){
alert("请输入用户昵称!"); //如果没输入用户名,则弹出一个提示框提示未输入用户名
form.usernc.focus(); //重新使用户昵称输入框获取焦点
return(false);
}
if(form.userpwd1.value==""){
alert("请输入注册密码!");
form.userpwd1.focus();
return(false);
}
if(form.userpwd2.value==""){
alert("请输入确认密码!");
form.userpwd2.focus();
return(false);
}
if(form.userpwd1.value!=form.userpwd2.value){ //判断密码与确认密码是否相同
alert("注册密码于确认密码不同!");
form.userpwd1.focus();
return(false);
}
if(form.userpwd1.value.length<6){ //判断密码长度是否大于或等于6位
alert("注册密码应大于6位!");
form.userpwd1.focus();
return(false);
}
if(form.truename.value==""){
alert("请输入真实姓名!");
form.truename.focus();
return(false);
}
if(form.sex.value==""){
alert("请选择性别!");
form.sex.focus();
return(false);
}
if(form.email.value==""){
alert("请输入E-mail地址!");
form.email.focus();
return(false);
}
if(form.email.value.match(/^(.+)@(.+)$/)==null){ //判断邮件地址的格式是否正确
alert("请输入正确的E-mail地址!");
form.email.focus();
return(false);
}
if(form.tel.value==""){
&nb
补充:Web开发 , php ,
|