Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.