Page 1 of 1

Show edited variable

Posted: Tue Jan 19, 2010 4:06 am
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

Re: Show edited variable

Posted: Thu Jan 21, 2010 2:03 am
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>