www.webdeveloper.com
Results 1 to 6 of 6

Thread: JavaScript College work

  1. #1
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226

    JavaScript College work

    Hey guys,

    I need some help with a college assessment. This is basic stuff, but we have been asked to validate a form with JavaScript before sending it to PHP.

    This is my first year with JS so I totally suck at it.

    I have this function.

    Code:
    function validate(title, forename, surname, street, town, postcode, email, age)
    {
    	if (title == false)
    	alert("Title is blank")
    	
    	if (surname == false)
    	alert("Surname is blank")
    	
    	if (street == false)
    	alert("Street is blank")
    	
    	if (town == false)
    	alert("Town is blank")
    	
    	if (postcode == false)
    	alert("Postcode is blank")
    	
    	if (email == false)
    	alert("Email is blank")
    	
    	if (age == false)
    	alert("Age is blank")	
    }
    Clearly this is not the way to do things, the function runs but always returns "Title is blank".

    I have no ideas.

    Thanks in advance.

    Alistair
    1888.1985 2110.2012

  2. #2
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    It depends on how the form is defined. If each field has an id then you can reference the value of the field using te id.

    For example

    <input type="text" id="title" value="">

    The value of this field can be accessed as document.getElementById('title').value

    You would then test for what the value contains rather than using == false.
    Stephen

  3. #3
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Would you then store the value in a variable once you have it by using document.detElementByID.

    Then you could use

    if (var == ' ')
    alert('Error')

    ?
    1888.1985 2110.2012

  4. #4
    Join Date
    Nov 2008
    Posts
    7
    You should know what are the parameters of the function.If they are the value of objects. You can write:
    Code:
    if(title == '')
      alert("Title is blank.");
    If they are the id of objects. You can write:
    Code:
    if(document.getElementById(title).value == '')
      alert("Title is blank.");

  5. #5
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    Angry Homework Help (JavaScript PICK-4 Lottery)

    <html>
    <!-- This page repeatedly simulate PICK-4 lottery drawings until a specific sequence is obtained. -->
    <!--============================================================================================ -->
    <!-- This page is designed to simulate the PICK-4 lottery, repeatedly until desired sequence is obtained. The user should be able to enter the desired sequence in a series of four text boxes, then click a button to see how many drawings the page had to perform before their sequence came up. This program will also make use of while loops for conditional execution, as well as implementing counter-driven loops. -->
    <head>
    <title> Pick 4 Lotto </title>

    <script type="text/javascript"
    src="http://www.dave-reed.com/book/random.js">
    </script>

    <script type="text/javascript">
    function RollRepeatedly()
    // Assumes: User will input desired sequence
    // Results: While loop will not execute until desired sequence is found. {

    /******** Declared Varibles ********/

    var roll1 = 0; roll2 = 0, PickNo = 0; repCount = 0;

    /******** End of Declaration ********/

    while (PickNo < 4) {
    roll1 = Math.floor(Math.random()*9+1);
    roll2 = Math.floor(Math.random()*9+1);

    if (roll1 == roll2) { document.getElementById('Pick'+PickNo).value = roll1; PickNo++; }
    repCount = repCount + 1;

    // alert(repCount+' : '+roll1+' : '+roll2); // for testing only
    }
    document.getElementById('reps').value = repCount;
    }
    </script>

    </head>
    <body>

    <div style="text-align:center">

    <h1>Pick 4 Lotto</h1>

    <form name="LottoForm">
    This page demonstrates the futility of lotteries.<br />
    Click on this button to perform Lotto drawings until <input type="text" id="pick0" name="reps0" size="1" value="0" />
    <input type="text" id="pick1" name="reps1" size="1" value="0" />
    <input type="text" id="pick2" name="reps2" size="1" value="0" />
    <input type="text" id="pick3" name="reps3" size="1" value="0" /> appears.

    <p />
    <input type="button" value="Click to begin drawing" onClick="RollRepeatedly();" />

    <p />
    Number of picks : <input id="reps" name="reps" type="text" size="6" value="0" />

    </form>
    </div>
    </body>
    </html>

  6. #6
    Join Date
    Nov 2008
    Location
    New Mexico, Albuquerque
    Posts
    25

    ESP-2 Test

    Okay I have another code for you to tackle. This one was from a previous chapter and I have been working on it for weeks.
    Create a web page that allows the user to conduct an ESP test. Statistics concerning the number and percentage of correct guesses should be maintained in text boxes. When the user clicks on one of these buttons, the corresponding guess should be passed as an input to the PickShape function. For the pictures, the rectangle containing the question mark represents the back of a card that is associated with a particular shape. After contemplating which shapes appears on the front of the card, the user clicks the button corresponding to the shape he or she believes is on the card (here, four possibilities are assumed). After each guess, the question mark should be replaced by a randomly selected shape, and an alert box should notify the user to whether the guess was correct. Statistics concerning the number and percentage of correct guesses should be maintained in text boxes. After the statistics have been updated and the alert box has been closed, the page should restore the question mark image, enabling the user to enter another guess.

    Here is my code which has some syntax errors, and I cannot get the shapes to appear or the counter to function.

    Code:
    <html>
     <!-- ESP-2 Test.html      Exercise 11.17 page 212-213                                     -->
     <!-- This page allows the user to conduct an ESP test.					    -->   
     <!-- ==================================================================================== -->
    
     <!--     This page serves as an ESP test, with a button for each possible guess.  	   
              For each shape named button, a card is associated with that particular shape.    
              The user is directed to guess which shape lies beneath the question mark          
              rectangular card.An alert box will notify the user whether their guess was 	   
              correct or not.Statistics concerning the number of guesses the user has taken    
              correctly will be displayed in the available text boxes.                         -->                 
    
     <head>
       <title> ESP Test </title>
      
      
       <script type="text/javascript"
               src="http://dave-reed.com/book/random.js">
       
          /* This is the (above) source in which a general-purpose funtion is placed from a library, 
             and then loaded unto the page as needed. */
      
       </script>
    
       <script type="text/javascript">  
          
       function PickShape(guess) { 
          
          /* This function is intended to randomly select a shape using the RandomInt function, and 
    	 compare the shape with that of the one the user picks. *?
          
          /****** Varibles ******/
         
          var Shape; Correctcount = 0; TotalCount = 0;
          
          /****** End of Varibles ******/
         
         // Assumes: the user will try to pick or guess the corresponding shape
         // Results: picks a randomly selected shape and compares that to the shape the user picks.
    
            
       Shape = RandomInt(1,4);
       TotalCount = TotalCount + 1; 
    
       if (PickShape == 1)
    	{
         document.getElementById("Square").src ="http://www.dave-reed.com/book/Images/square.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    			
    	}
    	}
        else if (PickShape == 2)
    	{
    	document.getElementById("Circle").src ="http://www.dave-reed.com/book/Images/circle.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    	}
    	}
        else if (PickShape == 3)
            {
            document.getElementById("Star").src ="http://www.dave-reed.com/book/Images/star.gif";
            if(PickShape == guess)
            {
    		Correctcount = Correctcount + 1;
            }
    	}
        else if (PickShape == 4)
    	{
    	document.getElementById("Triangle").src ="http://www.dave-reed.com/book/Images/triangle.gif";
    	if(PickShape == guess)
    	{
    		Correctcount = Correctcount + 1;
    	}
    	}
            document.getElementById("Correctcountbox").value = Correctcount;
    	
            numGuess = Correctcount/TotalCount;
            
    </script> 
     </head>
    
     <body>
        
       <h1 style="text-align:center">ESP Test</h1>
        <h2 style="text-align:center">Measure your Extrasensory Perception</h2>
     
        <p style="text-align:center">
    
     
                The card below hides the image of a square, triangle, circle, or star.<br />         
    Concentrate on the hidden image, then click on the corresponding button to make your guess.
        </p>
        <br />
        <p style="text-align:center">          
      Statistics showing the number and percentage of correct guesses will appear in the boxes.
        </p>
        <hr />
        
        <div style="text-align:center">
    
     <!-- This tag will allow there to be a button that when clicked will 'call' upon the value (function) specified in the onclick section. -->
                               
         
         <input type="button" value="Square" 
                onclick="PickShape(1);" />
         <input type="button" value="Triangle"
                onclick="PickShape(4);" />
         <input type="button" value="Circle"
                onclick="PickShape(2);" />
         <input type="button" value="Star"
                onclick="PickShape(3);" />
         <br />
         <p>
            <img id="mystery" alt="question mark" src="Images/mystery.gif" />
    		
         </p>
                You have guessed correctly <input type="text" id="Correctcount" size="10" value="0" /> out of <input type="text" id="TotalCount" size="10" value="0" /> times.
     
         
         <br />  
       
     
                That means you were correct<input type="text" id="numGuess" size="10" value="0" />% of the time.
    
     <!-- This is the end of the program and webpage. -->
       
        
        
       </div>
     </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles