...

View Full Version : Javascript Form Input Question



theflyingminstr
08-20-2007, 10:49 PM
Hi I have a question, it's probably very basic but I'm still learning the Javascript basics.

The following form outputs a string change (from "input" to "text" box ). When the word "test" is inputted, it changes to "good".

Simple question: If there is no matching string, how would I get the form to just display nothing at all?

Thanks!


<html>

<head>

<center>
<script type = "text/javascript">

function replaceChars() {
var temp = document.subform.input.value;

temp = temp.replace(/test/gi, "good");
temp = temp.replace (/hello/gi, "bad");
temp = temp.replace (/today/gi, "tomorrow");

document.subform.text.value = temp;
}

</script>

</head>

<body>


<form name="subform">


<FONT size=+2>
<textarea rows="8" name="input" cols="34" value=""></textarea></FONT>
<br><br>
<input type=button name=action value="Change" onClick="replaceChars(document.subform.input.value);">
<input type="reset" value="Reset" name="B2">

<br><br>

<table border="1" width="30%" id="table1" height="68" bgcolor="#FFFFFF">
<tr>
<td align="center" width="70"><INPUT TYPE="button" NAME="t" VALUE=" t " OnClick="subform.input.value += 't'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="e" VALUE=" e " OnClick="subform.input.value += 'e'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="s" VALUE=" s " OnClick="subform.input.value += 's'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="t" VALUE=" t " OnClick="subform.input.value += 't'"></td> </tr>
</table>


<br><br>

<textarea rows="8" name="text" cols="34" value=""></textarea>

</form>

</center>

</body>

</html>

Philip M
08-21-2007, 09:06 AM
function replaceChars() {
var temp = document.subform.input.value;

if (temp.toLowerCase() == "test" || temp == "hello" || temp == "today") {

temp = temp.replace(/test/gi, "good");
temp = temp.replace (/hello/gi, "bad");
temp = temp.replace (/today/gi, "tomorrow");
}

else {
temp.replace(/.+/g, "")
}

document.subform.text.value = temp;
}

Is there any point at all to this? I don't think that your users will see it as hilariously amusing to have "test" replaced by "good" or whatever.

theflyingminstr
08-21-2007, 05:10 PM
I'm sorry Philip, I'm just posting whatever code is necessary for the immediate question I am asking (so as not to confuse anyone with whatever code is in between that would be irrelevant to the topic). I'm trying to make a simple foreign character translation system actually.

About this top part though Philip:

if (temp.toLowerCase() == "test" || temp == "hello" || temp == "today") {

If I have a lot more words that would need to be in this top area of the code, is there a simpler way to write this?

Thanks so much for responding!

Philip M
08-21-2007, 09:55 PM
You can do something like this:-

function testForWord() {
var count = 0;

cmp = "word1 word2 word3 word4 .......... word n "; // note the final space which is essential. All words to be in lower case.

txt = document.formName.elementName.value;
txtlc = txt.toLowerCase;
for (var i=0; i<60; i++) { // 60 is the number of words above - must be counted accurately!! Obviously you can have as many words as you want, within reason.
pos = cmp.indexOf(" ");
wrd = cmp.substring(0,pos);
wrdl = wrd.length;
cmp = cmp.substring(pos+1,cmp.length);

while (txtlc.indexOf(wrd) >-1) {
count ++; // count gives the number of words in txt which are matched in cmp. In this case at least one required.
} // end of while
} // end of for
} // end of function

theflyingminstr
08-21-2007, 10:33 PM
Hey thanks Philip, cool code! The onlly question I have is can I use delimiters (,'s - :'s?) in this section:

cmp = "word1 word2 word3 word4 .......... word n "; /

for multiple word/phrase translations?


Thanks again!


- Mendi

Philip M
08-22-2007, 08:55 AM
Hey thanks Philip, cool code! The onlly question I have is can I use delimiters (,'s - :'s?) in this section:

cmp = "word1 word2 word3 word4 .......... word n "; /

for multiple word/phrase translations?


Thanks again!


- Mendi


The delimiter is the space. So a word can include an apostrophe or a hyphen, but cannot include a space (i.e two or more words cannot be regarded as one).

As I have said, you seem to be trying to run before you can even crawl. Your knowledge of JavaScript appears to be far too limited to be able to do this effectively (and, if I may say so, without a great deal of support from this forum). You say "Hi I have a question, it's probably very basic but I'm still learning the Javascript basics". Quite so! And there are already quite a few free language translation services out there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum