Show edited variable

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Marinusjvv
Forum Commoner
Posts: 29
Joined: Wed Dec 02, 2009 5:59 am

Show edited variable

Post by Marinusjvv »

Hi. I'm new to Javascript, and am trying to use it for form validation. My code is:

Code: Select all

<script type="text/javascript">
var b = "";
function checkNumber()
{
    var a = document.getElementById('amount').value
    if(isNaN(a))
    {
    b = "Value must be a number";
    }
}
</script>
Then on the actual page i have:

Code: Select all

<input type="text" name="amount" id="amount" onchange="checkNumber()"/>
<script type="text/javascript">
document.write(b);
</script>   
Now my problem is that it does not show anything for the variable 'b' (because it is set to "" when the page is loaded). Do i need to reload the page to be able to show the value for b? How would i do this?

Thanks
Marinusjvv
Forum Commoner
Posts: 29
Joined: Wed Dec 02, 2009 5:59 am

Re: Show edited variable

Post by Marinusjvv »

I have heard of another method of showing my results on the page: by use of a div tag. However, this doesn't work. Is there something wrong in my code?

Code: Select all

 
function checkNumber()
{
    var a = document.getElementById('amount').value
    if(isNaN(a))
    {
    document.getElementById('writeme').value = "Value must be a number";
    b = "Value must be a number";
    alert(b); // just to check that the function is working... It does!
    }
}
 
<div id="writeme"></div>
 
Post Reply