Disable form submit with illegal words

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jacotheron
    New Member
    • Jan 2008
    • 44

    Disable form submit with illegal words

    I need a script that can disable the submit button on a form or prevent the form from being submitted when there is an illegal word or character in the text box. This should prevent a spam bot from sending links to me via my website's contact me function. Here is the form that I need to protect:
    [HTML]
    <form id="FormName" action="thehand ler.cgi" method="post" name="FormName" >

    <p>Name: /Naam: <input type="text" name="name" size="24" /></p>
    <p>Email: /E-pos: <input type="text" name="email" size="24" /></p>
    <p>Message: /Boodskap:<br />
    <textarea name="message" rows="4" cols="30"></textarea></p>
    <p><input type="submit" name="Submit/stuur" value="Submit/Stuur" /></p>
    </form>
    [/HTML]

    The message textarea is the field that should be checked. Unnecessary text were removed.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    It won't prevent spam bots. Someone could just disable JavaScript.

    Anyhow, the way to do this is in the normal validation way:
    [code=html]<form ... onsubmit="retur n validate()">[/code]then validate() would return false if there are illegal words (and thereby prevent submission) and true otherwise.

    Comment

    • RamananKalirajan
      Contributor
      • Mar 2008
      • 608

      #3
      Hi, what did you mean by Illegal words, You can validate the form as Mr. Coder suggested or if you want to be more specific. onKeyPress event for TextArea write your own logic, so that the user can only enter character without special symbol and numbers. This alone can be done u can't check what he entered is meaningful or not while the user enters the text over there. If any doubts post it back I will try to help you out

      Regards
      Ramanan Kalirajan

      Comment

      • Jacotheron
        New Member
        • Jan 2008
        • 44

        #4
        Originally posted by RamananKaliraja n
        Hi, what did you mean by Illegal words, You can validate the form as Mr. Coder suggested or if you want to be more specific. onKeyPress event for TextArea write your own logic, so that the user can only enter character without special symbol and numbers. This alone can be done u can't check what he entered is meaningful or not while the user enters the text over there. If any doubts post it back I will try to help you out

        Regards
        Ramanan Kalirajan
        The illegal words/characters are to be specified in JS and be validated prior to form submit. How do I tell JS to check for it in the text area? That is where my problem is. The characters are: "<>?[]" The idea is to prevent them from sending me links to other sites i don't have time to visit. (these messages are about 120 k each to download and every day there are a lot of them)

        Thank you for the help

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Use a regular expression, e.g. [<>\?\[\]], to test the input from the text area using the match() method.

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            Originally posted by Jacotheron
            The illegal words/characters are to be specified in JS and be validated prior to form submit. How do I tell JS to check for it in the text area? That is where my problem is. The characters are: "<>?[]" The idea is to prevent them from sending me links to other sites i don't have time to visit. (these messages are about 120 k each to download and every day there are a lot of them)

            Thank you for the help
            Just check this one. This may the thing what u are looking for

            [HTML]<html>
            <textarea onkeypress="ret urn checkThis(event )">
            </textarea>
            </html>
            <script type="text/javascript">
            function checkThis(e)
            {
            var ch = e.keyCode;
            if(ch!=60&&ch!= 62&&ch!=63&&ch! =91&&ch!=93)
            return true;
            return false;
            }
            </script>[/HTML]

            Regards
            Ramanan Kalirajan

            Comment

            • Jacotheron
              New Member
              • Jan 2008
              • 44

              #7
              Originally posted by RamananKaliraja n
              Just check this one. This may the thing what u are looking for

              [HTML]<html>
              <textarea onkeypress="ret urn checkThis(event )">
              </textarea>
              </html>
              <script type="text/javascript">
              function checkThis(e)
              {
              var ch = e.keyCode;
              if(ch!=60&&ch!= 62&&ch!=63&&ch! =91&&ch!=93)
              return true;
              return false;
              }
              </script>[/HTML]
              I tried this script, but for some reason it does not prevent the use of illegal characters and it still gets through.

              Thank you for the help

              Comment

              • RamananKalirajan
                Contributor
                • Mar 2008
                • 608

                #8
                Originally posted by Jacotheron
                I tried this script, but for some reason it does not prevent the use of illegal characters and it still gets through.

                Thank you for the help
                Hello wether the code is showing any error. Or it doesn't restrict the illegal characters. The code was working fine for me. Please tell the problem u faced

                Regards
                Ramanan Kalirajan

                Comment

                • Jacotheron
                  New Member
                  • Jan 2008
                  • 44

                  #9
                  Originally posted by RamananKaliraja n
                  Hello wether the code is showing any error. Or it doesn't restrict the illegal characters. The code was working fine for me. Please tell the problem u faced
                  No code error is displayed and the submission is not prevented when entering the illegal characters.

                  Thank You for the help

                  Comment

                  • RamananKalirajan
                    Contributor
                    • Mar 2008
                    • 608

                    #10
                    Originally posted by Jacotheron
                    No code error is displayed and the submission is not prevented when entering the illegal characters.

                    Thank You for the help

                    I dont know why that code was not working for you. But any how try this one. This amy surely help u out.

                    [HTML]<html>
                    <head>
                    <script type="text/javascript">
                    var flag=0;
                    function check(ths)
                    {
                    var x = new Array("<",">"," ?","[","]");
                    var data = ths.value;
                    for(var i=0;i<x.length; i++)
                    {
                    if(data.indexOf (x[i])>0)
                    {
                    alert("coming") ;
                    flag=1;
                    break;
                    }
                    flag=0;
                    }
                    if(flag==1)
                    document.getEle mentById('mysub mit').disabled= true;
                    else
                    document.getEle mentById('mysub mit').disabled= false;
                    }
                    </script>
                    </head>
                    <form action="demo.ht ml">
                    <textarea onkeyup="check( this)">
                    </textarea>
                    <br/>
                    <input type="submit" value="submit" id="mySubmit">
                    </form>
                    </html>[/HTML]

                    Regards
                    Ramanan Kalirajan

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      with the suggested regExp by acoder you could cut down the entire thing to the following simple function:

                      [HTML]<html>
                      <head>
                      <script type="text/javascript">
                      function check(node) {
                      var re = new RegExp('[<>\?\\[\\]]', 'g');
                      document.getEle mentById('mySub mit').disabled = re.test(node.va lue);
                      }
                      </script>
                      </head>
                      <form action="demo.ht ml">
                      <textarea onkeyup="check( this)">
                      </textarea>
                      <br/>
                      <input type="submit" value="submit" id="mySubmit"/>
                      </form>
                      </html>[/HTML]
                      kind regards

                      Comment

                      • Jacotheron
                        New Member
                        • Jan 2008
                        • 44

                        #12
                        Originally posted by gits
                        with the suggested regExp by acoder you could cut down the entire thing to the following simple function:

                        [HTML]<html>
                        <head>
                        <script type="text/javascript">
                        function check(node) {
                        var re = new RegExp('[<>\?\\[\\]]', 'g');
                        document.getEle mentById('mySub mit').disabled = re.test(node.va lue);
                        }
                        </script>
                        </head>
                        <form action="demo.ht ml">
                        <textarea onkeyup="check( this)">
                        </textarea>
                        <br/>
                        <input type="submit" value="submit" id="mySubmit"/>
                        </form>
                        </html>[/HTML]
                        This form works now!

                        Thank You all for helping e solve this problem

                        Kind regards
                        Jacotheron

                        Comment

                        Working...