Since you posted the question, I have been experimenting with your problem for an hour using different methods. It seems to me that through an event at the entrance you cannot concentrate on yourself.
However, there is another possible workaround. You can call displayResult()
when submitting the form, check if text1
empty, and cancel sending if it's empty, and set focus to text1
. I rewrote the code for you.
<html> <head> <script type="text/javascript"> function displayResult() { var inp1=document.getElementById("text1").value; var inp2=inp1.length; if(inp2==0) { alert("Field 1 cannot be left Empty"); document.getElementById("text1").focus(); return false; } } </script> </head> <body> <form onsubmit="return displayResult();"> <input type="text" name="text1" id="text1" /> <input type="text" name="yext2" id="text2" /> <input type="submit" /> </form> </body> </html>
Hope this helps ...
Peace to you...
source share