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 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Grrr!!! Validator problem

    What am I doing wrong?
    The program works until I try to add the validator.
    BTW there are more functions beside m_to_k
    I still have a bunch more to do on this homework and this is driving me nutz!
    The HTML is the easy part!
    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
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    var MINNUM = <1;
    var MAXNUM = >1000000;


    Not valid syntax. Omit the < and >.

    Your error console ought to have revealed that.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 11-30-2012 at 08:25 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts

    what?

    <1 means less that one
    >100000 means more than 1000000
    instructor uses that all the time

  • #4
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I forgot to say that #1 I am still up working on this and I did make a correction I forgot the parenthesis around the <1 & the >1000000
    So it does look like this (but still does not work):
    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;
        };
    When I insert a letter instead of a digit I get this:
    meters equals NaNa kilometers you have traveled
    instead of this:
    Invalid digit entered. Must be between 1 & 1000000.
    and
    it is in the same place as where the calculation is and not an alert box
    BTW
    Anyway to change the color of the font there in the calculation?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    One more tine - the value of a numeric variable must be a number and may not include signs such as < or >. Whether or not in brackets. If your tutor says otherwise, get a new tutor.

    Code:
    <script type="text/javascript">
    
    function validator(meters) {
    var MINNUM = 1;
    var MAXNUM = 100000;
    meters = meters *1;  // make meters a number
    if ((isNaN(meters)) || (meters < MINNUM) || (meters > MAXNUM))    {
    alert("Invalid value entered. Must be a number between 1 & 1000000.");
    return false;
    }
    }
    
    validator(-1);  // a number out of range
    validator("xyz");  // a string
    
    </script>
    You can of course have if (meters > 100) or whatever.

    You cannot change the font or other styling of the built-in alert box. Use a <span> styled with CSS instead.
    Last edited by Philip M; 11-30-2012 at 02:20 PM. Reason: typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I removed my validation and inserted your validation and it does not work

  • #7
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I have not gone to sleep all night long racking my brains out on this and have to leave for school in a little while
    Here is the entire code.
    Everything worked until I put in the validation code
    Code:
    <script type="text/javascript">
    
    function validator(meters) {
    var MINNUM = 1;
    var MAXNUM = 100000;
    meters = meters *1;  // make meters a number
    if ((isNaN(meters)) || (meters < MINNUM) || (meters > MAXNUM))    {
    alert("Invalid value entered. Must be a number between 1 & 1000000.");
    return false;
    };
    }
    
    validator(-1);  // a number out of range
    validator("xyz");  // a string
    
    </script>
    	
    	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); 
    	};

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,302
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Quote Originally Posted by msNewbie View Post
    I removed my validation and inserted your validation and it does not work
    Well, it works for me, and it will work for you if you test it. The fault, dear Brutus, lies not in the stars ...

    Perhaps </script>
    is the problem.

    Code:
    var please = false;
    var thanks = false;
    if (!please && !thanks) {
    var interestLost = true;
    var moreHelp = 0;
    }

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    msNewbie (11-30-2012)

  • #9
    New Coder
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I am not ungrateful I am just so tired and I am sitting here in class on break right now trying to keep my eyes open.
    I will see what is wrong.
    YES, THANK YOU for your time & consideration in this matter!

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    </script>  <<< this is finishing the script
    	
    	var report = function(meters,kilometers) <<< it should occur at the end of your code
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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