Jump to content

Need help with my JS code


hazz995

Recommended Posts

Alright just started learning JS and I'm having a few problems.

I'm trying to make some form validation code with the help of a timer but the page keeps going blank every time it runs (with the exception of the text that is told to be outputted).

 

<script type="text/javascript">
	var usererror = ""; //NULL STRING
	var passerror = ""; //NULL STRING

	function CheckUsername()
	{	
		usererror = "Test";
	}
	function UpdateError()
	{
		document.write(usererror);
		t=setTimeout("UpdateError()",1000);
	}
</script>

</head>
<body>

<div class = "template">
<form action = "" method = "post" name = "formlogin">
	<table>
		<tr>
			<td width = '30%'>Username</td>
			<td width = '40%'><input type = "textfield" name = "username" onchange = "CheckUsername()"/></td>
			<td width = '30%' style = "color:red;">
				<script type="text/javascript">
					UpdateError();
				</script>				
			</td>
		</tr>
	</table>
</form>
</div>

 

Basically what I'm trying to tell it to do is to output the correct text when an error is made within the username textfield but it's obviously not working and I have no more ideas on how to fix it.

Sure the code is not complete and doesn't show the exact errors yet but I'll get to that once I get it to output text correctly...

 

Thanks ;)

Link to comment
https://forums.phpfreaks.com/topic/235627-need-help-with-my-js-code/
Share on other sites

<script type="text/javascript">

var usererror = ""; //NULL STRING

var passerror = ""; //NULL STRING

 

function CheckUsername()

{

var usererror = "Test";

                        document.getElementById("showError").innerHTML = usererror;

}

function UpdateError()

{

setTimeout("UpdateError()",1000);

}

</script>

 

</head>

<body>

 

<div class = "template">

<form action = "" method = "post" name = "formlogin">

<table>

<tr>

<td width = '30%'>Username</td>

<td width = '40%'><input type = "textfield" name = "username" onchange = "UpdateError();"/></td>

<td width = '30%' style = "color:red;" id="showError"> 

</td>

</tr>

</table>

</form>

</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.