简体   繁体   English

使用JavaScript进行表单验证。

[英]Form Validation Using JavaScript.

<html>
<head>
    <title>
        Form created using HTML
    </title>
</head>
<script>
        function validateform()
        {
            var x=document.forms["myForm"]["txtName"];
            var y=document.forms["myForm"]["txtAge"];
            if(x.value=="")
            {
                window.alert("Please enter valid values");
                return false;
            }
            else if(isNaN(y.value))
            {
                window.alert("Please enter valid values");
                return false;
            }
        }
        </script>
<body>
    <form name="myForm" action="/valid.php" onsubmit="return validateForm()" method="post">
        Employee Name : <input type="text" name="txtName"><br>
        Salary: <input type="text" name="txtAge"><br>
        <button type="submit" name="btnOK" value="Submit">OK</button>
        <button type="reset" name="btnClear" >Clear</button>
    </form>
</body>

I'm trying to validate my form but it just won't get executed. 我正在尝试验证我的表格,但它不会被执行。 I don't want to do it with "Id" method. 我不想使用“ Id”方法。 Sorry if I offended some of you with my last line, I'm fairly new in Javascript. 抱歉,如果我在上一行中冒犯了某些人,那么Java语言是我的新手。

You might need to return true from the function in case of successful execution in the end. 如果最终成功执行,则可能需要从函数返回true。 This might help: HTML form action and onsubmit issues 这可能会有所帮助: HTML表单操作和onsubmit问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM