Home > HTML, JavaScript And CSS Help > Textarea validation

Textarea validation



hi,



i have a textarea and i want the user to enter some text if he doesnt enter any text i want to fire an alert



the condition i am checking is in textarea if u press the spacebar it adds spaces i dont want them to add spaces and submit the form









here is my code




Code:



var checkbody=Trim(document.frm1.txtbody.value);

if(checkbody.length < 0)
{
alert("enter some text");
return false;
}




but the alert is never fired am i using the trim function properly



any ideas



thanks,



todd

    
Guest


There is no 'Trim()' function in JavaScript, so no, you are not

using it right.



You have to roll your own.

Code:


function trim(s)
{
return s.replace(/^\s*|\s*$/g,'');
}






Also, you are checking to see if the string has negative length,

you should be checking to see if it has 0 length.

Was this answer helpful ? Yes No   
Guest
<Script Language='JavaScript'>
//-- Start to check input data -------------------------
<!--
function isAskSubmit(form){
if (form.message.value.length < 3) {
alert ('Please fill in your message at least 3 charactors, thanks!');
form.message.focus();
return false;
}

if (form.message.value.length > 400) {
alert ('Please fill in your message max 400 charactors, thanks!');
form.message.focus();
return false;
}


return true;
}

//--End to check input data ----------------------------------
//-->
</Script>

<form name="ask" action="ask.php" method="post" onSubmit="return isAskSubmit(this)">
TEXT:<textarea name=message rows=8 cols=50></textarea>
<input type=submit>
</form>
Was this answer helpful ? Yes No   
Charles
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2010 by Infoqu. All rights reserved