hazz995 Posted May 5, 2011 Share Posted May 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235627-need-help-with-my-js-code/ Share on other sites More sharing options...
sunfighter Posted May 6, 2011 Share Posted May 6, 2011 You are writing to the page so, yes, it over writes what there. Try using alert('error code what you want to see'). Or make a div on your page and write to the div. Quote Link to comment https://forums.phpfreaks.com/topic/235627-need-help-with-my-js-code/#findComment-1211294 Share on other sites More sharing options...
aruns Posted May 6, 2011 Share Posted May 6, 2011 <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> Quote Link to comment https://forums.phpfreaks.com/topic/235627-need-help-with-my-js-code/#findComment-1211330 Share on other sites More sharing options...
hazz995 Posted May 6, 2011 Author Share Posted May 6, 2011 Woo thanks guys, solved! Never knew you could write to div tags with JS. Quote Link to comment https://forums.phpfreaks.com/topic/235627-need-help-with-my-js-code/#findComment-1211400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.