Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html表单中的年龄验证有问题_Javascript_Html_Forms_Validation - Fatal编程技术网

Javascript html表单中的年龄验证有问题

Javascript html表单中的年龄验证有问题,javascript,html,forms,validation,Javascript,Html,Forms,Validation,我想打印出来的段落标记输入正常,只会闪烁一秒钟,然后消失 <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <meta http-equiv='X-UA-Compatible' content='IE=edge'> <title>HOMEWORK</title> <meta name='viewport' content

我想打印出来的段落标记输入正常,只会闪烁一秒钟,然后消失

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>HOMEWORK</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
   </head>
    <body>
    <h1 style="text-align:center">FORM FILL</h1>
    <p>Kindly fill in the following details as mentioned below : </p>

    <form name="FORM"  onsubmit="checkform()">

       <!---- <label for="FName">Name :</label><br>
        <input type="text" id="FName" name="FName"><br><br>--->
        <label for="Age">Age :</label><br><br>
        <input type="text" id="Age" name="Age"><br><br>
        <input type="button" value="Submit">
    </form><br><br>
    <p id="demo"></p>

    <script>
        function checkform() 
        {
            var x, text;
            x = document.forms["FORM"]["Age"].value;
            if (x<1||x>100||x==''||isNaN(x))
                {
                    alert("Invalid Age !!!");
                    text = "Input not valid";
                   // return false;
                } 
            else
                {
                    text = "Input OK";
                   // return true;
                } 
            document.getElementById("demo").innerHTML = text;
        }
    </script>
</body>
</html>

作业
填表
请填写以下详细信息,如下所述:

年龄:





函数检查表() { 变量x,文本; x=文件.forms[“FORM”][“Age”].值; 如果(x100 | | x=''| | isNaN(x)) { 警告(“无效年龄!!!”; text=“输入无效”; //返回false; } 其他的 { text=“输入确定”; //返回true; } document.getElementById(“demo”).innerHTML=text; }
有人能告诉我我做错了什么吗?
验证表单的最短方法是什么?

您需要防止重定向页面,对代码做了一些更改,请检查一下:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>HOMEWORK</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
   </head>
    <body>
    <h1 style="text-align:center">FORM FILL</h1>
    <p>Kindly fill in the following details as mentioned below : </p>

    <form name="FORM"  onsubmit="return mySubmitFunction(event)">

       <!---- <label for="FName">Name :</label><br>
        <input type="text" id="FName" name="FName"><br><br>--->
        <label for="Age">Age :</label><br><br>
        <input type="text" id="Age" name="Age"><br><br>
        <input type="submit" value="Submit">
    </form><br><br>
    <p id="demo"></p>

    <script>
    function mySubmitFunction(e) { 
         e.preventDefault(); 
         try {
          checkform();
             } catch (e) {
                 throw new Error(e.message);
                    }
            return false;
        }
        function checkform() 
        {

            var x, text;
            x = document.forms["FORM"]["Age"].value;
            if (x<1||x>100||x==''||isNaN(x))
                {
                    alert("Invalid Age123 !!!");
                    text = "Input not valid";
                   // return false;
                } 
            else
                {
                    text = "Input OK";
                   // return true;
                } 
           document.getElementById("demo").innerHTML = text;
        }

    </script>
</body>
</html>

作业
填表
请填写以下详细信息,如下所述:

年龄:





函数mySubmitFunction(e){ e、 预防默认值(); 试一试{ checkform(); }捕获(e){ 抛出新错误(例如消息); } 返回false; } 函数检查表() { 变量x,文本; x=文件.forms[“FORM”][“Age”].值; 如果(x100 | | x=''| | isNaN(x)) { 警告(“无效年龄123!!!”; text=“输入无效”; //返回false; } 其他的 { text=“输入确定”; //返回true; } document.getElementById(“demo”).innerHTML=text; }
希望这有帮助