Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Form Input Question

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 08-21-2007 at 08:23 AM.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you

    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!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Cool

    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by theflyingminstr View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •