简体   繁体   中英

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. Sorry if I offended some of you with my last line, I'm fairly new in Javascript.

You might need to return true from the function in case of successful execution in the end. This might help: HTML form action and onsubmit issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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