zoukankan      html  css  js  c++  java
  • 表单信息的验证-客户端的验证

    表单信息的验证-客户端的验证

      利用JavaScript脚本语言进行客户端的表单提交的信息进行验证,步骤:

      1).在jsp页面的<head></head>中添加<script></script>脚本段,在该脚本段中定义信息判断的函数function1(),在函数中要获取提交的信息(两种方式获取表单提交的信息:1.在表单控件中添加ID值,应用的函数是:document.getElementById("ID-value")。2.根据每个表单控件的name来获取信息,应用的函数是:document.getElementById("name-value"))。

      2).在定义的函数中进行相应的信息判断,例如:if......else......语句,while语句。

      3).定义的function1()函数要有boolean型的返回值,当返回false时表单提交的信息不会提交到服务器端,当返回true时表单中提交的信息会提交到服务器端。

      4).在<form>的属性中进行上面定义函数的注册<form onsubmit = "return function1()">

    simple instance:

    <html>
      <head>
          <title>formConfirm</title>
        <script type = "text/javascript">
            function validate()
            {
                var username = document.getElementById("username1");
                var password = document.getElementById("password1");
                var repassword = document.getElementById("repassword1");
                if(username.value == "")
                {
                    alert("username can't be permit blank");
                    return false;
                }
                if(password.value.length < 6)
                {
                    alert("The length of password less than six");
                    return false;
                }
                if(repassword.value.length < 6)
                {
                    alert("The length of password less than six");
                    return false;
                }
                if(password.value != repassword.value)
                {
                    alert("password is not same with repassword");
                    return false;
                }
                return true;
            }    
        </script>
      </head>  
      <body>
          <form onsubmit = "return validate()">
              username:<input type = "text" name = "username" id = "username1"><br>
            password:<input type = "password" name = "password" id = "password1"><br>
               repassword:<input type = "password" name = "password" id = "repassword1"><br>
            <input type = "submit" name = button value = "提交">
          </form>   
      </body>
    </html>

  • 相关阅读:
    Notice: Only variable references should be returned by reference(PHP版本兼容性问题)
    App 开发:Hybrid 架构下的 HTML5 应用加速方案
    Hybrid App是如何实现网页语言与程序语言的混合?谁占主体?
    前端切图+网页排版的注意事项和经验分享
    php提示 Notice: Use of undefined constant name
    如何预测一个互联网产品的未来—一套关于产品的数学模型
    以 MAMP 为 Mac OS X 安装并设置 PHP开发环境
    关于EINTR错误的理解【转】
    socket中的函数遇见EINTR的处理【转】
    Ubuntu10.04中利用V4L2读取摄像头数据并保存成文件【转】
  • 原文地址:https://www.cnblogs.com/workhard-no-hardwork/p/3629341.html
Copyright © 2011-2022 走看看