What is wrong here?

Hi, can you please tell me why this script will not work. when i click submit, it goes straight to the action of the form, not the javascript.  Thanks




<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function checksame()
{
document.password.submit.disabled = true;

  if (document.password.old.value == "") {
    alert('Please enter your old password');
    document.password.old.focus();
    document.password.submit.disabled = false;
    return false;
  }
  if (document.password.new.length < 5) {
    alert('Your password must be at least 5 characters long');
    document.password.new.focus();
    document.password.new.select();
    document.password.submit.disabled = false;
    return false;
  }
  if (document.password.new.value != document.password.old.value) {
    alert('Your password must be at least 5 characters long');
    document.password.new.focus();
    document.password.new.select();
    document.password.submit.disabled = false;
    return false;
  }
}
</SCRIPT>
<LINK REL=STYLESHEET TYPE="text/css"
HREF="style.css">
<title>
Change Password
</title>
</head>
<body link="CCCCCC" vlink="CCCCCC" alink="CCCCCC">
<style type="text/css">
a:hover {color: 666666}
</style>
<h3>

<form name="password" id="password" action="new.asp" onsubmit="return checksame()">
<table cellpadding=5px class=h3>
<tr><td>Old Password:</td><td><input type="text" name="old1"></tr></td>
<tr><td>New Password:</td><td><input type="text" name="new"></tr></td>
<tr><td>Confirm Password:</td><td><input type="text" name="confirm"></tr></td>
<tr><td><input type="submit" value="Submit" name="submit"></td><td><input type="reset" value="Reset"></tr></td>
</form>
</table>


Thanks
-Kyle
LVL 2
kylen1010Asked:
Who is Participating?
 
SirCroftyCommented:
Your statements are not the problem. It is your use of field names. Do not use keywords to name your input fields. This line is causing a problem:

if (document.password.new.length < 5)

new is a reserved keyword, if you are going to use it as a field name, use: document.getElementById();
0
 
SirCroftyCommented:
<input type="submit" value="Submit" name="submit" onclick="return checksame();">

Do that instead of in the form tag.
0
 
radarshCommented:
onsubmit should be

onsubmit="javascript:checksame();"

You can also try one more thing.

Make your button of type "button" and give an onclick="javascript:checksame();"
there. In your checksame() function, you can do document.password.submit(); as the
last line.

Hope that's clear.
________
radarsh
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kylen1010Author Commented:
hi, i tried all three of those things  both of you mentioned, and none of them seemed to work.  that makes me think that there is an error somewhere in the script.  If I take out the 2nd two if statements, and just leave the first (old == "") it works fine an gives me the alert, but if i add one of the others, it will just go directly to the form action. thank you
0
 
radarshCommented:
>>     document.password.new.select();

What is that line in the two last if statements?
Remove those lines and it'll mostly work.

________
radarsh
0
 
kylen1010Author Commented:
i tried taking that out of the last 2 if statements, but no success
0
 
kylen1010Author Commented:
YEA!!! Thank you very very much SirCrofty.  That was exactly the problem. Now it works like a charm.  thank you!

-Kyle
0
 
jplevyakCommented:
Three things:
   1) Change the "new" field to a different name. This is a javascript keyword which will conflict.
   2.) You are referencing a different name in the javascript than the real field name "old" = "old1"
   3.) You need to specify new.value.length for the line  "if (document.password.new.length < 5) {", otherwise it will look for the number of fields with name "new" instead of the actual text content.

I found that whenever a form submit goes to a page and disregards any onSubmit validation, it usually means something is wrong in the validation script. The problem is the browser simply passes by the validation and you don't have anytime to see a status of the error before it immediately redirects to the action URL.

Try code below:
----------------------------------------------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
function checksame()
{
document.password.submit.disabled = true;

  if (document.password.old1.value == "") {
    alert('Please enter your old password');
    document.password.old1.focus();
    document.password.submit.disabled = false;
    return false;
  }
  if (document.password.new1.value.length < 5) {
    alert('Your password must be at least 5 characters long');
    document.password.new1.focus();
    document.password.new1.select();
    document.password.submit.disabled = false;
    return false;
  }
  if (document.password.new1.value != document.password.old1.value) {
    alert('Your password must be at least 5 characters long');
    document.password.new1.focus();
    document.password.new1.select();
    document.password.submit.disabled = false;
    return false;
  }
}
</SCRIPT>
<LINK REL=STYLESHEET TYPE="text/css"
HREF="style.css">
<title>
Change Password
</title>
</head>
<body link="CCCCCC" vlink="CCCCCC" alink="CCCCCC">
<style type="text/css">
a:hover {color: 666666}
</style>
<h3>

<form name="password" id="password" action="new.asp" onsubmit="return checksame()">
<table cellpadding=5px class=h3>
<tr><td>Old Password:</td><td><input type="text" name="old1"></tr></td>
<tr><td>New Password:</td><td><input type="text" name="new1"></tr></td>
<tr><td>Confirm Password:</td><td><input type="text" name="confirm"></tr></td>
<tr><td><input type="submit" value="Submit" name="submit"></td><td><input type="reset" value="Reset"></tr></td>
</form>
</table>
----------------------------------------------------------------------------------------
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.