Web Hosting Talk







View Full Version : Why won’t this Java Script Work?


fbsd4me
02-24-2004, 03:16 PM
Greetings.

This is a snippet I found that forces new members to use an alphanumeric password. Actually, this script works exactly as it should, except… It insists on calling my cgi script “EVEN” if the wrong value in entered. In other words, instead of halting the process, it displays the alert message, but when you click “OK”, it calls the script anyway. This sort of defeats the purpose.

I know very little about Java scripting, other than how to cut and paste whatever I can find. Something simple here is missing. Would anyone know what that is? Have a look:

<script language="Javascript">
function chkFormat(){
var bad=0;
var req='';

if ((document.myform.password_1.value.length < 6)||(document.myform.password_1.value.length >10)){
req = req + 'Password must be 6-10 characters\n';
bad=1;}

var re_alphanum = /^[a-zA-Z0-9]{6,10}$/;
if (!re_alphanum.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, non-alphanumeric characters: '+ document.myform.password_1.value+'\n';
bad=1;}

var re_alpha = /[a-zA-Z]/;
if (!re_alpha.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, One alphanumeric character is required: '+ document.myform.password_1.value+'\n';
bad=1;}

var re_num = /[0-9]/;
if (!re_num.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, One numeric character is required: '+ document.myform.password_1.value+'\n';
bad=1;}

if (bad){
alert('The Following Problems were Encountered:\n'+req);
return false;
}else{
alert('Good Password');
return true;
}
}
</script>


<body bgcolor="#FFFFFF" text="#000000">
<form name="myform" method="post" action="http://mydomain.com/cgi-bin/script.cgi" java script:void(0);" onsubmit="chkFormat();">
<input type="text" name="password_1">
<input type="submit" name="Submit" value="Submit">
</form>


Would really appriciate any help,

Dave H

Loon
02-24-2004, 03:28 PM
Replace:

onsubmit="chkFormat();"

With:

onsubmit="return chkFormat();"

At the moment it's just running the function rather than returning the true/false value.

Protollix
02-24-2004, 03:29 PM
You might try changing the "submit" button to just a button instead of a submit type.

Then add a onclick handler with a call to chkFormat()

then in chkFormat(), if everything is ok try document.myform.submit()

for example, this code works for me:
<script language="Javascript">
function chkFormat(){
var bad=0;
var req='';

if ((document.myform.password_1.value.length < 6)||(document.myform.password_1.value.length >10)){
req = req + 'Password must be 6-10 characters\n';
bad=1;}

var re_alphanum = /^[a-zA-Z0-9]{6,10}$/;
if (!re_alphanum.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, non-alphanumeric characters: '+ document.myform.password_1.$
bad=1;}

var re_alpha = /[a-zA-Z]/;
if (!re_alpha.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, One alphanumeric character is required: '+ document.myform.$
bad=1;}

var re_num = /[0-9]/;
if (!re_num.exec(document.myform.password_1.value)){
req = req + 'Invalid Password format, One numeric character is required: '+ document.myform.passw$
bad=1;}

if (bad){
alert('The Following Problems were Encountered:\n'+req);
return false;
}else{
alert('Good Password');
document.myform.submit();
}
}
</script>


<body bgcolor="#FFFFFF" text="#000000">
<form name="myform" method="post" action="http://mydomain.com/cgi-bin/script.cgi" java script:voi$
<input type="text" name="password_1">
<input type="button" name="Submit" value="Submit" onClick="chkFormat();">
</form>

Protollix
02-24-2004, 03:31 PM
Loon's suggestions works, and is much easier to implement ;)

fbsd4me
02-24-2004, 03:40 PM
An extended thanks to all of you for the fast and helpful responses. Instead of burning off several hours messing around, I should have broken down, and asked for help sooner. Loon, that did it. That one little change was all it needed.

Very much appreciated!

Dave H