• js表单_校验表单字段是否为空


    简单demo:

        1/校验输入是否为空

    <!DOCTYPE html>
    <html>
    
    <head>
        <script>
            // 如果某个表单字段(fname)是空的,那么该函数会发出一条警告消息,并返回 false,以防止表单被提交出去:
            function validateForm() {
                var x = document.forms["myForm"]["fname"].value;
                var y = document.forms["myForm"]["fage"].value;
                if (x == "" || y == "") {
                    alert("不能为空!必须填写!");
                    return false;
                }
            }
        </script>
    </head>
    
    <body>
    
        <form name="myForm" action="/demo/action_page.php" onsubmit="return validateForm()" method="post">
            姓名:<input type="text" name="fname">
            年龄: <input type="text" name="fage">
            <input type="submit" value="提交">
        </form>
    
    </body>
    
    </html>
    

      2/校验输入的是否为数字

    <!DOCTYPE html>
    <html>
    <body>
    
    <h2>JavaScript 能够验证输入</h2>
    
    <p>请输入 1 与 10 之间的数:</p>
    
    <input id="numb">
    
    <button type="button" onclick="myFunction()">提交</button>
    
    <p id="demo"></p>
    
    <script>
    function myFunction() {
      var x, text;
    
      // 获取 id="numb" 的输入字段的值
      x = document.getElementById("numb").value;
    
      // isNaN() 函数用于检查其参数是否是非数字值。数字false,非数字为true
      if (isNaN(x) || x < 1 || x > 10) {
    
        text = "输入无效";
      } else {
          
        text = "输入有效";
      }
      document.getElementById("demo").innerHTML = text;
    }
    </script>
    
    </body>
    </html>
    

      

  • 相关阅读:
    如何用cmd命令加密文件夹
    C++异常处理
    STRTOK
    如何生成Detours.lib——Detours的使用准备
    学习C++心得与值得一看的书
    工作两年后的感悟
    MFC十八个简单问题转载
    程序员的五种非技术错误 转载
    用VC写DLL中"error LNK2005: _DllMain@12 already defined"的错误
    CxImage
  • 原文地址:https://www.cnblogs.com/a1-top/p/14518008.html
Copyright © 2020-2023  润新知