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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SirCroftyCommented:
<input type="submit" value="Submit" name="submit" onclick="return checksame();">

Do that instead of in the form tag.
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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

radarshCommented:
>>     document.password.new.select();

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

________
radarsh
kylen1010Author Commented:
i tried taking that out of the last 2 if statements, but no success
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();

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kylen1010Author Commented:
YEA!!! Thank you very very much SirCrofty.  That was exactly the problem. Now it works like a charm.  thank you!

-Kyle
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>
----------------------------------------------------------------------------------------
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.