Web Analytics Made Easy -
StatCounter if else problem - CodingForum

Announcement

Collapse
No announcement yet.

if else problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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") {
    [COLOR="Red"]if (((tempobj.type=="text"||[/COLOR]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; Oct 15, 2006, 08:00 PM.
    The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

  • #2
    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!||||

    Comment


    • #3
      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

      Comment


      • #4
        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; Oct 15, 2006, 08:08 PM.
        The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

        Comment


        • #5
          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;
               }
             }

          Comment


          • #6
            All works now - I have incorporated your update. Thanks.
            The difference between genius and stupidity is that genius has its limits. (Albert Einstein)

            Comment

            Working...
            X