Click to See Complete Forum and Search --> : JS Brainiac challenge


rogerjh
04-01-2003, 03:06 PM
Okay geniuses! Here is a problem to test your skill.

I have a script to test the number of characters in a textarea box. If over a set number, an error message is returned, and the number must be reduced below the limit for the form to send.

My problem is how can I get this to work with two textarea boxes in one form? i.e. I have two variables, but I want to perform the same character number check on each variable separately, as the user enters the information.

The two variables together are here:
http://pdacrsp.oregonstate.edu/jobdesctest3.html.

The correctly functioning script for one variable is here;
http://pdacrsp.oregonstate.edu/jobDtest.html

Please help:confused:

khalidali63
04-01-2003, 03:48 PM
Originally posted by rogerjh
Okay geniuses! Here is a problem to test your skill......


Excuse me?
on the other hand if you really needed help then for problem like this no genius requried.


function Process(){
var frm = document.form1;
var limit = 4;
var t1 =frm.text1.value;
var t2 = frm.text2.value;
alert(t1.substring(0,limit)+"\n"+t2.substring(0,limit))
}

Hope this helps

Cheers

Khalid

Ice3T
04-01-2003, 04:05 PM
you could also put this in the textarea's tags
onkeyup="this.value=this.value.substring(0,4)"

where 4 is the max allowed amount of characters

rogerjh
04-01-2003, 04:23 PM
Hey you guys! Thanks for your help!
Sorry, maybe I am being really dense... no I AM being really dense, but I do not see how exactly to incorporate these scripts into my HTML. Please could you show me how? For example, with these is it necessary to declare different variables for my textareas? And is it necessary to establish two different functions? (Can you tell I am a JS newbie?):rolleyes:

Here is the script as I have it now:

<html>
<head>
<title>JOB DESCRIPTION TEST 3</title>
<script type="text/javascript"><!--
var limit = 400;
function check() {
if(document.mainform.Description.value.length > limit) {
alert('Too much data!');
document.mainform.Description.focus();
return false; }
else
return true; }
//
var limit2 = 400;
function check() {
if(document.mainform.Qualifications.value.length > limit2) {
alert('Too much data!');
document.mainform.Qualifications.focus();
return false; }
else
return true; }
//
function update() {
var old = document.mainform.counter.value;
document.mainform.counter.value=document.mainform.Description.value.length;
if(document.mainform.counter.value > limit && old <= limit) {
alert('Too much data in the text box!');
if(document.styleSheets) {
document.mainform.counter.style.fontWeight = 'bold';
document.mainform.counter.style.color = '#ff0000'; } }
else if(document.mainform.counter.value <= limit && old > limit
&& document.styleSheets ) {
document.mainform.counter.style.fontWeight = 'normal';
document.mainform.counter.style.color = '#000000'; }
}
//
function update2() {
var old2 = document.mainform.counter.value;
document.mainform.counter.value=document.mainform.Qualifications.value.length;
if(document.mainform.counter.value > limit2 && old2 <= limit2) {
alert('Too much data in the text box!');
if(document.styleSheets) {
document.mainform.counter.style.fontWeight = 'bold';
document.mainform.counter.style.color = '#ff0000'; } }
else if(document.mainform.counter.value <= limit2 && old2 > limit2
&& document.styleSheets ) {
document.mainform.counter.style.fontWeight = 'normal';
document.mainform.counter.style.color = '#000000'; }
}
//
-->
</script>

</head>
<body>
<form action="http://oregonstate.edu/tools/formmail.php" method="post"
name="mainform" onsubmit="return check();">
<input type="hidden" name="recipient" value="harrirog@onid.orst.edu">

<p>Describe the opportunity (maximum 400 characters).</p>
<textarea cols="106" rows="5" name="Description" onkeyup="this.value=this.value.substring(0,4)" onkeyup="update();"></textarea><br>
<noscript>
<p><small>(If you used a JavaScript enabled browser, preferably supporting JavaScript version 1.2 or equivalent (as supported e.g. by Internet Explorer&nbsp;4,
Netscape Navigator&nbsp;4, and Opera&nbsp;5), you would have some help from the browser in trying to remain within the limit.)</small>
</noscript>
<script type="text/javascript" language="JavaScript1.2"><!--
document.write('Characters typed: <input '+
'type="text" size="3" name="counter" value=""'+
'readonly onfocus="this.form.Description.focus()"> (limit: '+
limit+')');
//--></script>

<p>Describe the qualifications (maximum 400 characters).</p>
<textarea cols="106" rows="5" name="Qualifications" onkeyup="this.value=this.value.substring(0,4)" onkeyup="update2();"></textarea><br>
<noscript>
<p><small>(If you used a JavaScript enabled browser, preferably supporting JavaScript version 1.2 or equivalent (as supported e.g. by Internet Explorer&nbsp;4,
Netscape Navigator&nbsp;4, and Opera&nbsp;5), you would have some help from the browser in trying to remain within the limit.)</small>
</noscript>
<script type="text/javascript" language="JavaScript1.2"><!--
document.write('Characters typed: <input '+
'type="text" size="3" name="counter" value=""'+
'readonly onfocus="this.form.Qualifications.focus()"> (limit2: '+
limit2+')');
//--></script>

<p><input type="submit" value="Send">

</form>
</body>
</html>

Ice3T
04-01-2003, 06:22 PM
what I cant figure out is why you have this:

<script type="text/javascript" language="JavaScript1.2"><!--
document.write('Characters typed: <input '+
'type="text" size="3" name="counter" value=""'+
'readonly onfocus="this.form.Qualifications.focus()"> (limit2: '+
limit2+')');
//--></script>


Also this:
<textarea cols="106" rows="5" name="Qualifications" onkeyup="this.value=this.value.substring(0,4)" onkeyup="update2();"></textarea><

should be like this

<textarea cols="106" rows="5" name="Qualifications" onkeyup="this.value=this.value.substring(0,400),update2();></textarea>


and your update2() function doesn't need to check if limit is exeeded because it cannot do that thanks to this:
onkeyup="this.value=this.value.substring(0,400)"

but you could retain a part of it if you still want the alert() to show (I wouldn't but its your call).

also give us more details on what it is you want to do when the limit is reached, do you whant to change the style of the text?