<html> <head> <script language=Javascript type="text/javascript"> function checkname(x) { if(x==""){ window.alert("you must enter a username") reg.username.select() reg.username.focus() } } function checkpass(y) { if(y==""){ window.alert("you must enter a password") reg.password.select() reg.password.focus() } } </script> </head> <body> <form name=reg method="POST" action="login.php"> <p> </p> <p><b><font size="5"> You must login first before you order any book</font></b></p> <p> Username <input type="text" name="username" size="20" onblur="checkname(document.reg.username.value);"></p> <p>Password <input type="text" name="password" size="20" onblur="checkpass(document.reg.password.value);"></p> </form> </body> </html> Code (markup): The warning message pop up non stoip,how to modify this code?
Use this code. The password field can be filled only after filling the username field. function checkpass(y) { if(y=="" && document.reg.username == ""){ window.alert("you must enter a password") reg.password.focus() } } Code (markup): Or else use the following code, if you do not want to set the selection back to the un filled field. function checkname(x) { if(x==""){ window.alert("you must enter a username") } } function checkpass(y) { if(y=="" && document.reg.username == ""){ window.alert("you must enter a password") } } Code (markup): BTW there is no need for using regi.field_name.select(), since the action is executed only if the field is empty. If it is empty, then there is nothing to select