hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Why won’t this Java Script Work?
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Why won’t this Java Script Work?

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 02-24-2004, 03:16 PM
fbsd4me fbsd4me is offline
Junior Guru Wannabe
 
Join Date: Apr 2002
Posts: 53

Why won’t this Java Script Work?


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

Reply With Quote


Sponsored Links
  #2  
Old 02-24-2004, 03:28 PM
Loon Loon is online now
Community Liaison
 
Join Date: Apr 2003
Location: London, UK
Posts: 2,974
Replace:

Code:
onsubmit="chkFormat();"
With:

Code:
onsubmit="return chkFormat();"
At the moment it's just running the function rather than returning the true/false value.

Reply With Quote
  #3  
Old 02-24-2004, 03:29 PM
Protollix Protollix is offline
Junior Guru
 
Join Date: Feb 2003
Location: Virginia
Posts: 237
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:
Code:
<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 - Sean Finkel
Email
Website
Shared Hosting :: Cloud Hosting

Reply With Quote
Sponsored Links
  #4  
Old 02-24-2004, 03:31 PM
Protollix Protollix is offline
Junior Guru
 
Join Date: Feb 2003
Location: Virginia
Posts: 237
Loon's suggestions works, and is much easier to implement

__________________
Protollix - Sean Finkel
Email
Website
Shared Hosting :: Cloud Hosting

Reply With Quote
  #5  
Old 02-24-2004, 03:40 PM
fbsd4me fbsd4me is offline
Junior Guru Wannabe
 
Join Date: Apr 2002
Posts: 53
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

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?