php开发留言板教程之使用js来判断提交数据合法性

QQ截图20161130145506.png

使用js来判断提交数据正确性

<script>
     function CheckPost() {
         if(myform.user.value=="")
         {
             alert("请填写用户");
             myform.user.focus();
             return false;
         }
         if (myform.title.value.length<5)
         {
             alert("标题不能少于5个字符");
             myform.title.focus();
         return false;
         }
         if (myform.content.value=="")
         {
             alert("内容不能为空");
             myform.content.focus();
             return false;
         }
     }
 </script>

在提交页面中加入js判断,可以验证提交表单的合法性,能减少错误的发生。


本章重点

1.使用js对用户标题内容进行判断和检测。

继续学习
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="css.css" rel="stylesheet" type="text/css">
<title>Title</title>
<?php include ("add.php")?>
</head>
<script>
function CheckPost() {
if(myform.user.value=="")
{
alert("");
myform.user.focus();
return false;
}
if (myform.title.value.length<5)
{
alert("5");
myform.title.focus();
return false;
}
if (myform.content.value=="")
{
alert("");
myform.content.focus();
return false;
}
}
</script>
<body>
<b> <a href="list.php"></a> </b>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
提交重置代码
图片放大关闭