Javascript Help?

trewyn15

New Member
Hey guys, writing a code for class, I think I have it probably 95% done, just need a little help finishing it up.

Here's what supposed to be done:

homework_zpsbe6b5927.jpg


Here's what I currently have:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <script language="JavaScript">
	
	function echo()
	{
	
	document.write
	
	var int = parseInt(document.inform.instr.value);
	var real = parseReal(document.inform.instr.value);
	var str = (document.inform.instr.value);
	
	if 
		{
		(0 <= int && int <=99 &&
		(0.0 <= real && real <= 10.0) &&
		(str != null))
		document.write(<h1>"All input is valid:"</h1>);
		("Int No. = " + document.getElementsByName("int")[0].value + "<br>" +
		"Real No. = " + document.getElementsByName("real")[0].value + "<br />" +
		"String Length = "  + document.getElementsByName("string")[0].length );
		return true;
		}
		
	else
		{
		window.alert("Form error, please try again!");
		return false;
		}
	
	}
	
    </script>
    
  </head>

 <body>
  <form name="inform" onsubmit="echo()" id="myform">
	Enter Int No: <input type="text" name="int" id="myinput" /> (0-99)<br />
	Enter Real No: <input type="text" name="real" /> (0.0-10.0)<br />
	Enter String:  <input type="text" name="string" /><br />
	<input type="submit" /><br />
  </form>
 </body>

</html>

As you can probably tell, I'm not getting the correct display. I think my problem is that my Variables are not stated correctly in the beginning, but I'm not 100% sure. Any help would be greatly appreciated!
 
Moved to general software.

This isn't your problem but I'd suggest putting IDs on your input fields and using document.getElementById. That way you only ever get 1 not an array.

Your error is at if { .....

The correct syntax is
Code:
if (conditions) {
  code;
}
edit:
So what you want to write is
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
    <script language="JavaScript">
	
	function echo()
	{
	var int = document.getElementsByName("int")[0].value;  //parseInt(document.inform.instr.value);
	var real = document.getElementsByName("real")[0].value //parseReal(document.inform.instr.value);
	var str = document.getElementsByName("string")[0].value.length //(document.inform.instr.value);
	
	if 
		(0 <= int && int <=99 &&
		(0.0 <= real && real <= 10.0) &&
		(str != null))
		{
		document.write("<h1>All input is valid:</h1><br>Int No. = " + document.getElementsByName("int")[0].value + "<br>" +
		"Real No. = " + document.getElementsByName("real")[0].value + "<br />" +
		"String Length = "  + document.getElementsByName("string")[0].value.length);
		}
		
	else
		{
		window.alert("Form error, please try again!");
		}
	return false;	
	}
	
    </script>
    
  </head>

 <body>
  <form name="inform" onsubmit="echo()" id="myform">
	Enter Int No: <input type="text" name="int" id="myinput" /> (0-99)<br />
	Enter Real No: <input type="text" name="real" /> (0.0-10.0)<br />
	Enter String:  <input type="text" name="string" /><br />
	<input type="submit" /><br />
  </form>
 </body>

</html>
I moved the return false to the very last thing and removed the return true. In a forms submit these have special actions. It tells the browser weather it should do its default action or not, i.e. submit the form
I also cleaned up some stuff that I didn't know what it was doing :P
 
Last edited:
Wow, thank you so much!

That works exactly like it should. I see what you did with the if, I had that extra curly bracket in there which was probably messing things up.

For the variables, is that just the easiest way to declare variables? It looks about the same as where the variables are actually used. It's not what we did in class but seems easier anyways
 
No, I didn't fix that part :)

Where you are referring to the values, you don't need to get the values from the source again (unless they changed, which they shouldn't have here).

This part could be written differently:
Code:
document.write("<h1>All input is valid:</h1><br>Int No. = " + document.getElementsByName("int")[0].value + "<br>" +
		"Real No. = " + document.getElementsByName("real")[0].value + "<br />" +
		"String Length = "  + document.getElementsByName("string")[0].value.length);
As
Code:
document.write("<h1>All input is valid:</h1><br>Int No. = " + int + "<br>" +
		"Real No. = " + real + "<br />" +
		"String Length = "  + str);
 
Back
Top