I need to learn to read...I wrote the below text before realizing how simple your question was! So, to firstly directly answer
Quote:
|
the function of length finds a bug when clicking cancel. Any ideas?
|
When the user cancels, the "vs" object is null, has no properties, and therefore has a zero length. So as the example below shows, all ye' gotta do is either surround everything with
if (vs != null) {
...code...
}
or add an
if (vs == null) {
return;
}
immediately after the user is prompted.
--
Hmm, I did some reformatting and changed all if's to if-else cases, as it seems that's what you would want as opposed to a dozen errors coming up (but I may be wrong!).
I tested the code below and all cases work - length, invalid pass, valid pass, canceling etc. The thing to note with your code, you always want to terminate a line with the '
;' character, even though it works without 'em, it's standard and allows commenting after.
Also, you only want to continue if there is no null object (IE: the user cancels), which is why I put the return in there.
Code:
<script type="text/javascript">
function pass() {
var vs = window.prompt("PASSWORD TO EDIT :","XXXXXX");
if (vs == null) {
// the user cancelled
return;
}
var ls = vs.length;
var vssnull = "XXXXXX";
var ss = "12369*"; //PASSWORD
if (ls < 5) {
alert("ERROR 1 : The password must be over 5 characters long.");
} else if (vs == vssnull) {
alert("ERROR 3 : NO PASSWORD INSERTED");
} else if (vs == ss) {
window.location = "http://i-plus.enosi.org/cgi-bin/util/fm_nmp";
} else {
alert("ERROR 4 : INVALID PASSWORD");
}
}
</script>
Hope that helps.