...

View Full Version : if else problem



tpeck
10-15-2006, 05:05 PM
I can't do this. See the bit in red, well, this being a checker to determine if something is being entered into a form field, it checks for whether the field is type "text". Fine, but I want to use this routine to do the same thing for another field that is of type "password".


function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;

Since the || operator means "or" I would have thought it would check a password field too by adding:

if (((tempobj.type=="text"||tempobj.type=="password"|| etc.

The entire checking routine is:


<!-- Begin
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(8,30).toUpperCase();
alert("Please make sure the "+shortFieldName+" field was properly completed.");
return false;
}
else
return true;
}
// End -->


What am I doing incorrectly?

Thanks,

_Aerospace_Eng_
10-15-2006, 07:13 PM
Can you post your html as well that you are using with the JS?

vinyl-junkie
10-15-2006, 07:53 PM
Also, please use wrap you code in code tags, to make it easier for us to distinguish code from your comments about it.

tpeck
10-15-2006, 11:58 PM
Oops! Didn't know what the code tags did! Obvious now. Always wondered about those windows. I've edited the previous post in this thread.


<html>

<head>
<title>Logon</title>
<script language="JavaScript">
function explain(name, output, msg) {
newwin = window.open('','','top=150,left=150,width=325,height=300');
if (!newwin.opener) newwin.opener = self;
with (newwin.document)
{
open();
write('<html>');
write('<body onLoad="document.form.box.focus()"><form name=form>' + '<font face="Arial"; style="font-size: 13px"; color="#000000">' + msg + '<br>');
write('<p>You may enter your ' + name + ' here and it will be copied into the form for you.');
write('<p><center>' + name + ': <input type=text name=box size=10 onKeyUp=' + output + '=this.value>');
write('<p><input type=button value="Click to close when finished" onClick=window.close()>');
write('</center></font></form></body></html>');
close();
}
}
</script>
<script>
function checkrequired(which) {
var pass=true;
if (document.images) {
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)=="required") {
if (((tempobj.type=="text"||tempobj.type=="password"||tempobj.type=="textarea")&&
tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(8,30).toUpperCase();
alert("Please make sure the "+shortFieldName+" field was properly completed.");
return false;
}
else
return true;
}
</script>
</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table2866">
<tr>
<td>Please enter your logon details:<p></p>
<table border="1" cellpadding="5" cellspacing="0" id="table2867" bgcolor="#FFFFCC" bordercolor="#CCCCCC">
<tr>
<form name="form" onsubmit="return checkrequired(this);checkWord()" action="banners.html?requiredusername" method="link">
<td align="right">Username:</td>
<td><input type="text" name="requiredusername" size="10"></td>
<td>
<a href="javascript:explain('User Name', 'opener.document.form.requiredusername.value', 'The Username field is where you enter the User name you were given when you signed up for an affiliate account. It is a number and is also referred to as your Affiliate ID.');" onmouseover="window.status='Click for explanation...';return true;" onmouseout="window.status='';return true;">
Help?</a></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="requiredpassword" size="10"></td>
<td>
<a href="javascript:explain('Password', 'opener.document.form.requiredpassword.value', 'The Password is the same password you were given when you signed up and were given an affiliate account. It is a combination of letters and numbers.');" onmouseover="window.status='Click for explanation...';return true;" onmouseout="window.status='';return true;">
Help?</a></td>
</tr>
</table>
<p><input type="submit" name="Enter" value="Send" tabindex="4">&nbsp;&nbsp;
<input type="reset" value="Clear" tabindex="5"></form></p>
</td>
</tr>
</table>

</body>

</html>

Now it seems to work(!), so it must be something else that is preventing things happening - probably one of the 7 other external js files interfering somehow. I'll try and track it down.

Thanks.

Arty Effem
10-16-2006, 12:39 AM
Now it seems to work(!), so it must be something else that is preventing things happening - probably one of the 7 other external js files interfering somehow. I'll try and track it down.

Thanks.I doubt it, probably just a typo. Either way, here's a more streamlined way of performing those tests:
if (/^required/.test(tempobj.name))
{
if( ((/text|textarea|password/.test(tempobj.type))&&
tempobj.value=='')||(/^select/.test(tempobj.type) && tempobj.selectedIndex<1))
{
pass=false;
break;
}
}

tpeck
10-16-2006, 02:05 AM
All works now - I have incorporated your update. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum