here is the code sample:
HTML:
<form id="information" name="information" action="#" method="post">
<textarea name="text" rows="10" cols="10">
</textarea>
<input type="submit" value="submit"/>
</form>
Javascript:
window.onload=init;
function init(){
document.getElementById('information').onsubmit=validateForm;
}
function validateForm(){
var text= document.information.text.value;
if(text==""){
alert('text area cannot be empty');
return false;
}
}
it does not work…
The textarea has a carriage return in it (and a space), so your condition is never true. So change it to:
However you may want to go further and stop the user entering an “empty” response (meaning nothing but white-space). Or you may just want to remove leading and trailing white-space. If so you can do this:
Now
trim()I believe is relatively new (Firefox lists it as new in 3.5). A more backwards compatible version is:and then testing if it’s empty.
Faster JavaScript Trim has a good analysis on various white-space trimming alternatives. For instance the above can be done with one regex:
but