Q:

validation is not working in javaScript

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form>
        Name:<input type="text" id="t1"> <br> Gender: <input
            type="radio" name="sex" value="male">male <input type="radio"
            name="sex" value="female">female <br> <input
            type="submit" value="submit" onClick="myFunction()">
    </form>

    <script type="text/javascript">
        function myFunction() {
            var str = document.getElementById("t1").value;
            if (str == "") {
                alert("plz enter anything");
            }
        }
    </script>

</body>
</html>

I want to validate when there is no input from user and if user is pressing submit button then it should alert "enter any thing" but it is not working.......why I don't know

A:

Your function isn't preventing the form from submitting. To do that, you need to return false when you want the form submission to stop, and also use onClick="return myFunction()" to make sure it's passed correctly.
Note however that in up-to-date browsers you can just do this:
<input type="text" required />

The browser will automatically stop form submission if nothing was entered.

