Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2004, 10:40 AM
Authorized User
 
Join Date: Jul 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default redirect page question

I want to realize follow function.There are one text type area and password type area in a form,after user filled both of them,this form will redirect another page.But if either of two is not filled,this form asks to fill it,and don't redirect another page.I use Javascript to check either of them is null,if it is null,it return false and this form don't redirect another page.But I found it doesn't work,when I don't fill either of them and click submit button,it still redirect another page.Why? My code is follows:

<script language="JavaScript">
<!--
function isEmpty(str)
{
 if ((str==null)||(str.length==0))
 {
  return true;
 }
 else
 {
  return false;
 }
}

function check()
{
 var nickName=window.document.form1.nickName.value;
 var pwd=window.document.form1.pwd.value;
 if(isEmpty(nickName))
 {
  alert("Input your name:");
  window.document.form1.nikeName.focus();
  return false;
 }
 if(isEmpty(pwd))
 {
  alert("Input your password:");
  window.document.form1.pwd.focus();
  return false;
 }
}
-->
</script>
<html>
<body bgcolor="#FFFFFF" text="#000000">
 <div align="center">
 <form name="form1" method="post" action="test.jsp" onsubmit="return check();">
  <table width="350" border="0" cellspacing="0">
   <tr>
    <td width="108" bgcolor="#ffd6c8" height="27">
     <div aligh="right">Name:</div>
    </td>
    <td width="238" bgcolor="#ffd6c8" height="27">
     <input type="text" name="nickName" size="20" maxlength="10">
    </td>
   </tr>
   <tr>
    <td width="108" bgcolor="#ffd6c8" height="27">
     <div aligh="right" >Password:</div>
    </td>
    <td width="238" bgcolor="#ffd6c8" height="27">
     <input type="password" name="pwd" size="20" maxlength="10">
    </td>
   </tr>
   <tr>
     <td width="108" bgcolor="#ffd6c8">&nbsp;</td>
     <td width="238" bgcolor="#ffd6c8">
      <input type="submit" name="Submit" value="OK">
      <input type="reset" name="reset" value="Cancle">
     </td>
    </tr>
   </table>
 </form>
 </div>
</body>
</html>

Any idea will be appreciated!
Edward

 
Old March 16th, 2004, 11:28 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Aside from one typo it worked fine, I added a 'return true' at the end of the check function as a matter of principle :)
Code:
<html>
<head>
<title>Validate</title>
<script language="JavaScript">
<!--
function isEmpty(str)
{
 if ((str==null)||(str.length==0))
 {
  return true;
 }
 else
 {
  return false;
 }
}

function check()
{
 var nickName=window.document.form1.nickName.value;
 var pwd=window.document.form1.pwd.value;
 if(isEmpty(nickName))
 {
  alert("Input your name:");
  window.document.form1.nickName.focus();
  return false;
 }
 if(isEmpty(pwd))
 {
  alert("Input your password:"); 
  window.document.form1.pwd.focus();
  return false;
 }
 return true;
}
-->
</script>
<html>
<body bgcolor="#FFFFFF" text="#000000">
 <div align="center">
 <form name="form1" method="post" action="about:blank" onsubmit="return check();">
  <table width="350" border="0" cellspacing="0">
   <tr>
    <td width="108" bgcolor="#ffd6c8" height="27">
     <div aligh="right">Name:</div>
    </td>
    <td width="238" bgcolor="#ffd6c8" height="27">
     <input type="text" name="nickName" size="20" maxlength="10">
    </td>
   </tr>
   <tr>
    <td width="108" bgcolor="#ffd6c8" height="27">
     <div aligh="right" >Password:</div>
    </td>
    <td width="238" bgcolor="#ffd6c8" height="27">
     <input type="password" name="pwd" size="20" maxlength="10">
    </td>
   </tr>
   <tr>
     <td width="108" bgcolor="#ffd6c8">&nbsp;</td>
     <td width="238" bgcolor="#ffd6c8">
      <input type="submit" name="Submit" value="OK">
      <input type="reset"  name="reset"  value="Cancle">
     </td>
    </tr>
   </table>
 </form>
 </div>       
</body>
</html>
--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect url question newbie07 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 August 22nd, 2007 12:48 PM
redirect to page other than default page sarah lee ASP.NET 1.0 and 1.1 Basics 3 December 15th, 2006 05:45 PM
Redirect to new page without closing current page peter2004 ASP.NET 2.0 Basics 5 June 5th, 2006 08:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.