提交和验证方法总结

提交和验证方法总结


1、使用submit按钮,结合onsubmit事件来实现(最常用)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php.cn</title>
<script type="text/javascript">
function checkForm()
{
    //判断用户名是否为空
    if(document.form1.username.value=="")
    {
        window.alert("用户名不能为空!");
        return false;
    }else
    {
        window.alert("验证通过!");
        return true;
    }
}
</script>
</head>
<body>
<form name="form1" method="post" action="login.php" onsubmit="return checkForm()">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="submit" value="提交表单" />
</form>
</body>
</html>



2、submit按钮,结合onclick事件,实现表单的验证和提交

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php.cn</title>
<script type="text/javascript">
function checkForm()
{
    //判断用户名是否为空
    if(document.form1.username.value=="")
    {
        window.alert("用户名不能为空!");
    }else
    {
        window.alert("验证通过!");
    }
}
</script>
</head>
<body>
<form name="form1" method="post" action="login.php">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="submit" value="提交表单" onclick="checkForm()" />
</form>
</body>
</html>



3、button按钮(普通按钮),结合submit()方法,实现表单验证提交

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php.cn</title>
<script type="text/javascript">
function checkForm()
{
    if(document.form1.username.value.length == 0)
    {
        //如果用户名为空
        window.alert("用户名不能为空!");
    }else if(document.form1.username.value.length<5 || document.form1.username.value.length>20)
    {
        //如果用户名长度小于5或大于20
        window.alert("用户名只能介于5-20个字符!");
    }else if(checkOtherChar(document.form1.username.value))
    {
        //如果用户名含有特殊符号
        window.alert("用户名中含有特殊符号!");
    }else
    {
        //如果验证通过,提交表单
        window.alert("验证通过!");
        //表单提交方法
        document.form1.submit();
    }
}
function checkOtherChar(str)
{
    //定义一个特殊符号的数组
    var arr = ["*","&","<",">","$","\\","/"];
    //循环比较:数组中的每一个字符,与用户名每一个字符进行比对
    for(var i=0;i<arr.length;i++)
    {
        for(var j=0;j<str.length;j++)
        {
            if(arr[i]==str.charAt(j))
            {
                return true;
            }
        }
    }
    //如果没找到
    return false;
}
</script>
</head>
<body>
<form name="form1" method="post" action="login.php">
用户名:<input type="text" name="username" />
密码:<input type="password" name="userpwd" />
<input type="button" value="提交按钮" onclick="checkForm()" />
</form>
</body>
</html>


继续学习
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>php.cn</title> <script type="text/javascript"> function checkForm() { //判断用户名是否为空 if(document.form1.username.value=="") { window.alert("用户名不能为空!"); return false; }else { window.alert("验证通过!"); return true; } } </script> </head> <body> <form name="form1" method="post" action="login.php" onsubmit="return checkForm()"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd" /> <input type="submit" value="提交表单" /> </form> </body> </html>
提交重置代码
章节
笔记
提问
课件
反馈
捐赠

javascript初级教程

高并发千万级数据库系统解决方案
  • 推荐课程
  • 评论
  • 问答
  • 笔记
  • 课件下载

Mr.L

你们都是大神 一看就会 就我一个晕头转向的

6年前    添加回复 0

php实习生

问题1:function checkOtherChar(str) 与function checkOtherChar()这个区别是?为什么在里面加了str? 问题2:麻烦帮我详细解释下两个for循环代表什么意思?arr.length是代表arr字符的长度,str.length是指出现特殊字符的长度吗?谢谢

7年前    添加回复 0

一辆想出轨的无轨电车

这个还是能看懂的。。。6666

7年前    添加回复 0

我又来了

这个有用

7年前    添加回复 0

我喜欢晴天

几种提交方式,其实实现的效果是一样的

7年前    添加回复 0

末日的春天

判断是否为空,PHP啊

7年前    添加回复 0

橱窗的光

我觉得button 给个事件,还比较合理,因为submit,当点击时,会自己跳转到另外一个页面!

7年前    添加回复 0

课件暂不提供下载,工作人员正在整理中,后期请多关注该课程~