0
<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.

Shebu
  • 1
  • 1
  • 1

1 Answers1

0

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

Kush
  • 755
  • 7
  • 22