Announcement

Collapse
No announcement yet.

javascript question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • javascript question

    Hello,

    I'm developing a website for my brother. In it will be an inputform, on which I like to have a number of checks. Using dynamic HTML, I have a system that indicates which fields are incorrect or blank next to the input field: there is a * next to the required input fields; if they are left blank or are incorrect, an errormessage is placed next to it.

    On the form is a radio button selector to select the ways of contacting when an order has arrived; choices are: telephone, fax, email, gsm, sms. I have a function that is called for each inputfield (for phone number, mail address, ...) and takes the radiobutton into account:
    Code:
    function CheckContact(medium) {
    verplicht = document.getElementById('b'+medium).checked; 
    //radio button that indicates medium: btel/bemail/bfax/bgsm/bsms
    //verplicht == true if the radio button for the medium is checked
    //verplict = Dutch for requiered
    
    s=document.getElementById(medium).value;
    // input box for the phone number, mailaddres, ... (named to match with medium)
    
    // POSITION1 (for my question) 
    
    // klaarmaken van foutboodschap
    if (medium=='email') document.getElementById('e_'+medium).innerHTML="E-mail address";
      else if (medium=='tel') document.getElementById('e_'+medium).innerHTML="Phone number";
      else if (medium=='fax') document.getElementById('e_'+medium).innerHTML="Fax number";
      else if (medium=='gsm') document.getElementById('e_'+medium).innerHTML="GSM number";
      
    if (s=='') { // blanco
      if (verplicht) { document.getElementById('e_'+medium).innerHTML+=" has to be provided"; return false; } 
     	        else { document.getElementById('e_'+medium).innerHTML=""; return true;}
      } else { // if blanco
      if (verplicht) { 
        if ((medium=='email' && (s.indexOf("@")<1)) || 
    	   ((medium=='tel' || medium=='fax') && (s.length<9)) ||
    		  (medium=='gsm' && (s.length<10))) { 
    			document.getElementById('e_'+medium).innerHTML+=" is incorrect"; return false; } // verplicht + fout
        else { document.getElementById('e_'+medium).innerHTML="*"; return true; }  // verplicht + correct
      } else { // if verplicht
    	  if ((medium=='email' && (s.indexOf("@")<1)) || 
    	     ((medium=='tel' || medium=='fax') && (s.length<9)) ||
    		  (medium=='gsm' && (s.length<10))) { 
    			document.getElementById('e_'+medium).innerHTML+=" is incorrect (leave blank if none)"; return false; } // foutief
          else { document.getElementById('e_'+medium).innerHTML=""; return true; }
        } // else verplicht
      } // else blanco
     
    }

    This above script works perfectly, only that it doesn't check for the mobile number when SMS is selected via the radio button.
    Now, in the case of SMS, I would it to do the exact same checks as for mobile. My first thought was to add the following line in 'position 1':
    Code:
    if (medium=='sms') medium='gsm';
    Oddly enough, this cause the check to work for sms, but no longer for gsm... .

    It still doesn't work if I rename the occurances of medium prior to 'position 1' to i.e. medium1 and change the above line to:
    Code:
    if (medium1=='sms') medium='gsm' else medium=medium1;
    I know I can add the whole SMS check with sufficiant if's and manually linking it with GSM (and it most likely will work), but I'm hoping to keep the code this compact. I'm stumped as to why my described attempt doesn't work...

    Any thoughts? Suggestions?
    Thanks!


    Jörg
    Last edited by VJ; 2 January 2006, 09:45.
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

  • #2
    I'm not familiar with javascript but why don't you use an OR statement:
    Code:
    if (medium=='sms' || medium=='gsm')
    Originally posted by VJ
    Code:
    if (medium=='sms') medium='gsm';
    Oddly enough, this cause the check to work for sms, but no longer for gsm... .
    Jörg
    I guess that javascript creates another object called medium (the first one is the button object the second one a new medium variable object) and thats the reason why it doesn't work.

    Another tip: try to write your comments in one language and not a mixture of dutch and english this makes it easier for other developers to understand your code.
    Last edited by KeiFront; 2 January 2006, 10:56.
    Main: Dual Xeon LV2.4Ghz@3.1Ghz | 3X21" | NVidia 6800 | 2Gb DDR | SCSI
    Second: Dual PIII 1GHz | 21" Monitor | G200MMS + Quadro 2 Pro | 512MB ECC SDRAM | SCSI
    Third: Apple G4 450Mhz | 21" Monitor | Radeon 8500 | 1,5Gb SDRAM | SCSI

    Comment


    • #3
      Originally posted by KeiFront
      I'm not familiar with javascript but why don't you use an OR statement:
      Code:
      if (medium=='sms' || medium=='gsm')
      I'll give it a go...


      Another tip: try to write your comments in one language and not a mixture of dutch and english this makes it easier for other developers to understand your code.
      All comments were originally in Dutch, I just added a few English comment to make it more readable when posted here.


      Jörg
      pixar
      Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

      Comment


      • #4
        Originally posted by KeiFront
        I'm not familiar with javascript but why don't you use an OR statement:
        Code:
        if (medium=='sms' || medium=='gsm')
        It yields the exact same issue: with this test, the check works for sms but no longer for gsm.


        Jörg
        pixar
        Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

        Comment


        • #5
          I don't know JS, but have you replaced all occurences of
          Code:
          medium=='gsm'
          with
          Code:
          medium=='gsm' || medium=='sms'
          ?

          To me it looks as if, if you only replaced the first occurence of this string, you do read the mobile number, but don't do any of the checks, because medium=='sms' is neither mail, nor tel, nor fax, nor gsm, and you don't check for that. Oh! You said you could include sms expressly in the checks, but would rather not to keep the code compact. Well, then obviously KeiFront's solution won't work for you.

          Idea: Use sms and gsm like you do now, do
          Code:
          if (medium=='sms' || medium=='gsm') medium='mobile'
          before POSITION1 and replace all occurrences of gsm with mobile after POSITION1. Does that work?
          There's an Opera in my macbook.

          Comment


          • #6
            I found it... The problem is with the definition of 'verplicht'. If I set assign it to
            Code:
            verplicht = document.getElementById('bgsm').checked || document.getElementById('bsms').checked;
            when medium=='sms', everything works ok...

            I think it defined it incorrectly on my form (possibly, the checkbutton for sms is also named gsm due to my copy/paste; but I'll have to look into it)....


            I'll check the form, but the problem wasn't where I thought it was... (I thought assigning to medium went wrong, but it is elsewhere).

            Thanks for the insights!


            Jörg
            pixar
            Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

            Comment

            Working...
            X