Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: if else problem

  1. #1
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts

    if else problem

    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".

    Code:
    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:

    Code:
    <!-- 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,
    Last edited by tpeck; 10-16-2006 at 12:00 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Can you post your html as well that you are using with the JS?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,088
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Also, please use wrap you code in code tags, to make it easier for us to distinguish code from your comments about it.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #4
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    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.

    Code:
    <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.
    Last edited by tpeck; 10-16-2006 at 12:08 AM.

  • #5
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by tpeck View Post

    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:
    Code:
    if (/^required/.test(tempobj.name))
       {
        if( ((/text|textarea|password/.test(tempobj.type))&&
            tempobj.value=='')||(/^select/.test(tempobj.type) && tempobj.selectedIndex<1))
         {
          pass=false;
          break;
         }
       }

  • #6
    Regular Coder tpeck's Avatar
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    817
    Thanks
    43
    Thanked 6 Times in 5 Posts
    All works now - I have incorporated your update. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •