/    Sign up×
Community /Pin to ProfileBookmark

textarea validation?

hello all,
i have the following code that has a textarea on the form and also 3 input text box’s i need to either expand the last (3rd) input text field to be a text area or have a function that checks for at least one charactor in the textare i have been having no luck simple expanding the 3rd input box to a textare
help thanks john

<html>

<head>
<script type=”text/javascript”>
function validate()
{
x=document.myForm
at=x.email.value.indexOf(“@”)
code=x.code.value
firstname=x.fname.value
submitOK=”True”

if (at==-1)
{
alert(“Not a valid e-mail!”)
submitOK=”False”
}

if (firstname.length<1)
{
alert(“Please…Your name must be at least 1 charactor long”)
submitOK=”False”
}

if (code.length<1)
{
alert(“Please enter a comment”)
submitOK=”False”
}

if (submitOK==”False”)
{
return false
}
}
</script>
</head>

<body>
<form name=”myForm” action=”hello.htm” onsubmit=”return validate()”>
<br>
I have a bike:
<input type=”checkbox” name=”Bike”>
<br>
I have a car:
<input type=”checkbox” name=”Car”>
<p></p>
Male:
<input type=”radio” checked=”checked”
name=”Sex” value=”male”>
<br>
Female:
<input type=”radio”
name=”Sex” value=”female”>
<p></p>
<select name=”cars”>
<option value=”volvo”>Volvo
<option value=”saab”>Saab
<option value=”fiat”>Fiat
<option value=”audi”>Audi
</select>

<p></p>

<textarea rows=”10″ cols=”30″>
Please enter a few coments.
</textarea>
<p></p>

Enter your e-mail: <input type=”text” name=”email” size=”20″><br />

Enter your name: <input type=”text” name=”fname” size=”20″><br />
Enter a comment: <input type=”text” name=”code” size=”20″><br />
<input type=”submit” value=”Submit”>
</form>
</body>

</html>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterSep 25.2005 — No idea what you are asking of us, but I think this is what you mean:
<i>
</i>&lt;textarea name="ta" rows="10" cols="30"&gt;Please enter a few coments.&lt;/textarea&gt;

(notice that the above is all on the same line -- no extra newlines)

<i>
</i>
if (x.ta.value=="Please enter a few coments.")
{
alert("Please enter a comment")
submitOK="False"
}
Copy linkTweet thisAlerts:
@jwisneski2003authorSep 26.2005 — hello ultimator,

sorry for the confusion, but i fixed what i needed with your help

<html>

<head>

<script type="text/javascript">

function validate()

{

x=document.myForm

at=x.email.value.indexOf("@")

code=x.code.value

firstname=x.fname.value

submitOK="True"

if (at==-1)

{

alert("Not a valid e-mail!")

submitOK="False"

}

if (firstname.length<1)

{

alert("Please...Your name MUST be at least 1 charactor long")

submitOK="False"

}


if (code.length<1)

{

alert("whoops Last Name To!!")

submitOK="False"

}

if (x.ta.value=="Please enter a few coments.")

{

alert("Please enter meaningful comments")

submitOK="False"

}


if (submitOK=="False")

{

return false

}

}

</script>

</head>

<body>

<form name="myForm" action="hello.htm" onsubmit="return validate()">

<br>

I have a bike:

<input type="checkbox" name="Bike">

<br>

I have a car:

<input type="checkbox" name="Car">

<p></p>

Male:

<input type="radio" checked="checked"

name="Sex" value="male">

<br>

Female:

<input type="radio"

name="Sex" value="female">

<p></p>

Pick a Car

<select name="cars">

<option value="volvo">Volvo

<option value="saab">Saab

<option value="fiat">Fiat

<option value="audi">Audi

</select>

<p></p>


<p></p>


Enter your e-mail: <input type="text" name="email" size="20"><br />

<p></p>

Enter First name : <input type="text" name="fname" size="20"><br />

<p></p>

Enter Last Name : <input type="text" name="code" size="20"><br />

<p></p>

<textarea name="ta" rows="10" cols="30">Please enter a few coments.</textarea>

<p>

</p>

<input type="submit" value="Submit">

</form>

</body>

</html>

thanks john
Copy linkTweet thisAlerts:
@deep_dhyaniSep 26.2005 — try this


<html>

<head>

<script type="text/javascript">

function validate()

{

x=document.myForm

at=x.email.value.indexOf("@")

code=x.code.value

firstname=x.fname.value

if (at==-1)
{
alert("Not a valid e-mail!")
return false;
}
if (firstname.length<1)
{
alert("Please...Your name MUST be at least 1 charactor long")
return false;
}
if (code.length<1)
{
alert("whoops Last Name To!!")
return false;
}
if (x.ta.value=="Please enter a few coments.")
{
alert("Please enter meaningful comments")
return false;
}
return true;

}

</script>

</head>

<body>

<form name="myForm" action="hello.htm" onsubmit="return validate()">

<br>

I have a bike:

<input type="checkbox" name="Bike">

<br>

I have a car:

<input type="checkbox" name="Car">

<p></p>

Male:

<input type="radio" checked="checked"

name="Sex" value="male">

<br>

Female:

<input type="radio"

name="Sex" value="female">

<p></p>

Pick a Car

<select name="cars">

<option value="volvo">Volvo

<option value="saab">Saab

<option value="fiat">Fiat

<option value="audi">Audi

</select>

<p></p>


<p></p>


Enter your e-mail: <input type="text" name="email" size="20"><br />

<p></p>

Enter First name : <input type="text" name="fname" size="20"><br />

<p></p>

Enter Last Name : <input type="text" name="code" size="20"><br />

<p></p>

<textarea name="ta" rows="10" cols="30">Please enter a few coments.</textarea>

<p>

</p>

<input type="submit" value="Submit">

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@UltimaterSep 26.2005 — jwisneski2003, all deep.dhyani did, in his/her above code, was change the "return true" and "return false" setup within your function which don't really matter as long as your code is readable to yourself.

I know you got what you want but you might also want to consider testing if the textarea is blank or contains only whitespaces:
<i>
</i> if (x.ta.value=="Please enter a few coments.")
{
alert("Please enter meaningful comments")
return false;
}
if (x.ta.value.replace(/[srn]/g,"")=="")
{
alert("Please enter meaningful comments")
return false;
}
Copy linkTweet thisAlerts:
@JerseyGunnerAug 17.2008 — Dear altimater,

Could you please describe what this line is doing? I know that the r is acarrage return and the n is th enew line. Wha tielse is the lline looking for?

Thanks

JerseyGunner

if (x.ta.value.replace(/[srn]/g,"")=="")
Copy linkTweet thisAlerts:
@yosedeshJan 19.2010 — In case anyone else has the same question:

Dear altimater,

Could you please describe what this line is doing? I know that the r is acarrage return and the n is th enew line. Wha tielse is the lline looking for?

Thanks

JerseyGunner

if (x.ta.value.replace(/[srn]/g,"")=="")[/QUOTE]


s is looking for any white space and actually encompasses r and n.

He is simply replacing any white space with nothing and then checking to see if an empty string is the result. Personally, I wouldn't do this directly with x.ta.value because it will make an actual message difficult to read.

You may find this useful:

http://www.javascriptkit.com/javatutors/redev.shtml

Good luck!
Copy linkTweet thisAlerts:
@Declan1991Jan 19.2010 — Personally, I wouldn't do this directly with x.ta.value because it will make an actual message difficult to read.[/QUOTE]
Strings are immutable, which means they cannot be changed. x.ta.value.replace will not alter the value of x.ta.value because that is impossible. Instead, it creates a new string.
Copy linkTweet thisAlerts:
@yosedeshFeb 12.2010 — Thanks for the correction Declan1991. x.ta.value will actually maintain its value.
×

Success!

Help @jwisneski2003 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...