www.webdeveloper.com
Results 1 to 8 of 8

Thread: JavaScript Validation

  1. #1
    Join Date
    Nov 2012
    Posts
    5

    JavaScript Validation

    What am I doing wrong?
    The program works until I try to add the validation.
    BTW there are more functions beside m_to_k
    Code:
    	function validator()
    	var MINNUM = <1;
        var MAXNUM = >1000000;
        //Validate the inputted meters
        while((isNaN(meters))	||	(meters < MINNUM)	||	(meters > MAXNUM))
        {
            alert("Invalid digit entered. Must be between 1 & 1000000.");
        return result;
        };
    	
    	var report = function(meters,kilometers)
    	{
    		document.getElementById("result").innerHTML = 	//"result" gets sent to the HTML page where it is being asked for
    		meters + " meters are equal to " + kilometers + " kilometers"; 
    		//is just the text that will show up on the monitor.  meters + is what the user typed in.  The space after the quotations will insert what was typed in by the user then the text shows up (meters are equal to). + kilometers + inserts the caculation below. The space after the quotations will insert what was caculated. Then the text shows up (kilometers). 
    	};
    
    		document.getElementById("m_to_k").onclick = function() // after the user types in the digits the user will click on the button of choice which will preform the caculation below.
    	{
    		var meters = document.getElementById("distance").value; //meters will be equal to what the user typed in and be sent to report	
    		var kilometers = (meters *.001); //kilometers will be equal to the caculation in parenthises and will send it to report
    	 
    	report(meters, kilometers); //this report has the information of the digits that were entered (meters) plus the caculation (kilometers) that will be grabbed by the HTL page to be displayed.
    	};

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You're missing curly brackets around your validator() function.

  3. #3
    Join Date
    Nov 2012
    Posts
    5

    still up all night long

    Quote Originally Posted by refreezed View Post
    You're missing curly brackets around your validator() function.
    I am still up trying to get this.
    Yes, I did see that and corrected it.
    I should have gotten back on the forum.
    When I type in a letter, less that 1 or more than 1000000 I get this: meters equals NaNa kilometers you have traveled
    instead of this: Invalid digit entered. Must be between 1 & 1000000.

  4. #4
    Join Date
    Jan 2010
    Posts
    77
    Hi bud

    First off your validator function is missing its set of curly braces, as refreezed mentioned secondly you cannot assign a logical operator to a variable that is to say your variable MIMIMUM cant equal less than 1 (<1) and you MAXIMUM cant equal greater than 1000000 (>1000000). I wouldn’t use a while loop either a simple if statement would work, and lastly you return “result” but result is not defined at least not within the scope of your validator function.

    Because you have just posted a fragment of your code it is difficult to see exactly what is wrong with it can you post the all the code for the page? This would help greatly

  5. #5
    Join Date
    Nov 2012
    Posts
    5
    The entire program runs except for the validation.
    Can you please fix this for me?
    I am seeing cross-eyed and have to get ready for school and haven't been to bed all night.
    Code:
    	function validator()
    	var MINNUM = (<1)
        var MAXNUM = (>1000000)
        //Validate the inputted meters
        while((isNaN(meters))	||	(meters < MINNUM)	||	(meters > MAXNUM))
        {
            alert("Invalid digit entered. Must be between 1 & 1000000.");
        return result;
        };
    	
    	var report = function(meters,kilometers)
    	{
    		document.getElementById("result").innerHTML = 	//"result" gets sent to the HTML page where it is being asked for
    		meters + " meters are equal to " + kilometers + " kilometers"; 
    		//is just the text that will show up on the monitor.  meters + is what the user typed in.  The space after the quotations will insert what was typed in by the user then the text shows up (meters are equal to). + kilometers + inserts the caculation below. The space after the quotations will insert what was caculated. Then the text shows up (kilometers). 
    	};
    
    		document.getElementById("m_to_k").onclick = function() // after the user types in the digits the user will click on the button of choice which will preform the caculation below.
    	{
    		var meters = document.getElementById("distance").value; //meters will be equal to what the user typed in and be sent to report	
    		var kilometers = (meters *.001); //kilometers will be equal to the caculation in parenthises and will send it to report
    	 
    	report(meters, kilometers); //this report has the information of the digits that were entered (meters) plus the caculation (kilometers) that will be grabbed by the HTL page to be displayed.
    	};
    
    		var report2 = function(meters,inches)
    	{
    	    document.getElementById("result").innerHTML = 	
    		meters + " meters are equal to " + inches + " inches"; 
    	};
    		document.getElementById("m_to_i").onclick = function() 
    	{
    		var meters = document.getElementById("distance").value; 
    		var inches = (meters * 39.37); 
    	 
    		report2(meters, inches); 
    	};
    
    		var report3 = function(meters,feet)
    	{
    	    document.getElementById("result").innerHTML = 	
    		meters + " meters are equal to " + feet + " feet"; 
    	};
    		document.getElementById("m_to_f").onclick = function() 
    	{
    		var meters = document.getElementById("distance").value; 
    		var feet = (meters * 3.281); 
    	 
    		report3(meters, feet); 
    	};

  6. #6
    Join Date
    Jan 2010
    Posts
    77
    Hi bud

    Here is the code for the meters to kilometres conversion you should be able to work out how to do the rest of your conversions from that

    Code:
    <html>
      <head>
        <script type="text/javascript">
    
        function validator(meters){   
        var MINNUM = 1;
        var MAXNUM = 1000000;
    
        if((isNaN(meters)) || (meters < MINNUM) || (meters > MAXNUM))
        {
            alert("Invalid digit entered. Must be between 1 & 1000000.");
            return false;
        }
        else
        {
            return true;
        }
                             }
        function metersToKilometers(){
            var meters = Number(document.getElementById("distance").value)
            if(validator(meters)){
               document.getElementById("result").innerHTML= meters/1000;
                                 }
                                     }
    
        </script>
      </head>
      <body>
        <label for="distance">Enter Distance : <input type="text" name="distance" id="distance"></label><br />
        <input type="button" value="Convert Meters to Kilometers" onclick="metersToKilometers()">
        <div id="result"></div>
    
      </body>
    </html>
    Hope this helps

    V

  7. #7
    Join Date
    Nov 2012
    Posts
    5
    I'll try it when I get to school.
    Thanks "V"
    MsNewbie

  8. #8
    Join Date
    Nov 2012
    Posts
    5

    Thank you ever so much!

    After I got a second wind today and looked at you code I realized what was going on. If you were here right now I would hug you! ( )
    I wound up splitting the code up into different sheets because of the way I wanted to present it and it looks fantastic with all the graphics and HTML stuff I threw in. YOU are a life saver. Thank you ever so much. One more project and I am done for the semester - THANK GOD!!! and that YOU too!

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