Javacript problem-Onblur problem,please help

Discussion in 'HTML & Website Design' started by cty2007, Mar 25, 2007.

  1. #1
    <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>&nbsp;</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?
     
    cty2007, Mar 25, 2007 IP
  2. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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 :)
     
    Aragorn, Mar 25, 2007 IP