We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

What is wrong here?

kylen1010
kylen1010 asked
on
Medium Priority
363 Views
Last Modified: 2006-11-18
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
Comment
Watch Question

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

Do that instead of in the form tag.

Commented:
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

Author

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

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

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

________
radarsh

Author

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
YEA!!! Thank you very very much SirCrofty.  That was exactly the problem. Now it works like a charm.  thank you!

-Kyle

Commented:
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>
----------------------------------------------------------------------------------------
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.