Click to See Complete Forum and Search --> : Can someone help me?


Tasmanian Devil
05-06-2003, 03:55 PM
Can someone help me out? I want this script to submit, and self close the window if the two passwords match and are longer than four characters. I have it to submit and close but not to stop if passwords do not match or are not four characters long. Here is what I got:

<head>
<SCRIPT LANGUAGE="JavaScript">
function validatePwd() {
var invalid = " "; // Invalid character is a space
var minLength = 4; // Minimum length
var pw1 = document.myForm.password.value;
var pw2 = document.myForm.password2.value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your password twice.');
return false;
}
// check for minimum length
if (document.myForm.password.value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.myForm.password.value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same password twice. Please re-enter your password.");
return false;
}
else {
alert('Thank you.');
return true;
}
}
}
</script>

<body>
<input type=button onclick="this.form.submit(); self.close()" value="Submit">


Thanks

Charles
05-06-2003, 04:26 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<form action="" onsubmit="if (this.first.value == this.second.value && /^\S{4}\S*$/.test(this.first.value)) {this.submit(); if (top.opener) top.close()} else {alert('Both \'Password\' fields must be the same, contain no spaces and be four or more characters in length.'); this.first.value=''; this.second.value=''}; return false">
<p><label>Password<br>
<input type="password" name="first"></label></p>
<p><label>Password<br>
<input type="password" name="second"></label></p>
<div><input type="submit"></div>
</form>

ejrhodes
05-06-2003, 04:29 PM
EDITED- DONT READ THE REST OF MY POST, IT IS USELESS COMPARED TO THE POST ABOVE =)


i may be and probably am blind, but do you call your password validate function before the close and submit calls?


I am new to javascript but can you add the submit and close as the final else in your validate function ie

if this is bad-stop
else
submit- close

Tasmanian Devil
05-07-2003, 12:33 PM
Charles~
I tried your solution and it gave me a syntex error.

ejrhodes~
I don't belive I did, and don't know how to either.

Thanks
Guys

Charles
05-07-2003, 02:00 PM
Yes, the board changed my posting a bit and now there are two syntax errors. Correct them and all will be well.

Tasmanian Devil
05-07-2003, 02:22 PM
how do I fix the syntax errors?