HOW TO: *Not* Validate Spaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wayfarer

    #1

    HOW TO: *Not* Validate Spaces

    Yes, I Googled, but I didn't find an entry to this specific
    problem in the first couple of result pages.

    I've been using the following script to validate form entry.
    Some nefarious individuals have figured out a way to submit the
    form by entering spaces in the required text fields (imagine
    that!). Is there any way to plug up this hole that ain't
    browser-specific?

    TIA

    Neill

    function CheckForm ()
    {
    var errorMsg = "";
    if (document.frmMe ssage.name.valu e == '')
    {
    errorMsg += "\n\tName \t- Enter your Name";
    }
    if (document.frmMe ssage.message.v alue == '')
    {
    errorMsg += "\n\tMessag e \t\t- Enter a message";
    }
    if (errorMsg != ""){
    msg =
    "______________ _______________ _______________ _______________ ___\n\n";
    msg += "Your message has not been sent because there are
    problem(s) with the form.\n";
    msg += "Please correct the problem(s) and re-submit the form.\n";
    msg +=
    "______________ _______________ _______________ _______________ ___\n\n";
    msg += "The following field(s) need to be corrected: -\n";

    errorMsg += alert(msg + errorMsg + "\n\n");
    return false;
    }
    return true;
    }


    --
    Wayfarer
    Journeys: http://www.journeys.ws/

    There is a difference between knowing the path and walking the
    path. - Morpheus
  • News Me

    #2
    Re: HOW TO: *Not* Validate Spaces

    Wayfarer wrote:[color=blue]
    > Yes, I Googled, but I didn't find an entry to this specific
    > problem in the first couple of result pages.
    >
    > I've been using the following script to validate form entry.
    > Some nefarious individuals have figured out a way to submit the
    > form by entering spaces in the required text fields (imagine
    > that!). Is there any way to plug up this hole that ain't
    > browser-specific?
    >
    > TIA
    >
    > Neill
    >
    > function CheckForm ()
    > {
    > var errorMsg = "";
    > if (document.frmMe ssage.name.valu e == '')
    > {
    > errorMsg += "\n\tName \t- Enter your Name";
    > }
    > if (document.frmMe ssage.message.v alue == '')
    > {
    > errorMsg += "\n\tMessag e \t\t- Enter a message";
    > }
    > if (errorMsg != ""){
    > msg =
    > "______________ _______________ _______________ _______________ ___\n\n";
    > msg += "Your message has not been sent because there are
    > problem(s) with the form.\n";
    > msg += "Please correct the problem(s) and re-submit the form.\n";
    > msg +=
    > "______________ _______________ _______________ _______________ ___\n\n";
    > msg += "The following field(s) need to be corrected: -\n";
    >
    > errorMsg += alert(msg + errorMsg + "\n\n");
    > return false;
    > }
    > return true;
    > }
    >
    >[/color]

    do a Google Groups search for "javascript trim", you'll get pages of
    examples...

    NM

    Comment

    • Lee

      #3
      Re: HOW TO: *Not* Validate Spaces

      Wayfarer said:[color=blue]
      >
      >Yes, I Googled, but I didn't find an entry to this specific
      >problem in the first couple of result pages.
      >
      >I've been using the following script to validate form entry.
      >Some nefarious individuals have figured out a way to submit the
      >form by entering spaces in the required text fields (imagine
      >that!). Is there any way to plug up this hole that ain't
      >browser-specific?
      >
      >TIA
      >
      >Neill
      >
      >function CheckForm ()
      >{
      >var errorMsg = "";
      >if (document.frmMe ssage.name.valu e == '')[/color]

      if (-1==document.frm Message.name.va lue.search(/\S/)) {
      // field is empty or contains only blanks
      }

      However, you still need to validate the data on the server side,
      because nefarious individuals will always be able to bypass any
      client-side validation.

      Comment

      • MyndPhlyp

        #4
        Re: HOW TO: *Not* Validate Spaces


        "Wayfarer" <wfping2004-x@yahoo.com> wrote in message
        news:1103587816 .6f722d302d8046 1a72d42b9f8a29f 3c1@teranews...[color=blue]
        > Yes, I Googled, but I didn't find an entry to this specific
        > problem in the first couple of result pages.
        >
        > I've been using the following script to validate form entry.
        > Some nefarious individuals have figured out a way to submit the
        > form by entering spaces in the required text fields (imagine
        > that!). Is there any way to plug up this hole that ain't
        > browser-specific?[/color]

        function isblank(s)
        {
        for(var i = 0; i < s.length; i++)
        {
        var c = s.charAt(i);
        if ((c != ' ') && (c != "\n") && (c != "\t")) return false;
        }
        return true;
        }

        Client-side scripting can be circumvented.


        Comment

        • Martin Bialasinski

          #5
          Re: HOW TO: *Not* Validate Spaces

          Wayfarer <wfping2004-x@yahoo.com> wrote:
          [color=blue]
          > Some nefarious individuals have figured out a way to submit the
          > form by entering spaces in the required text fields (imagine
          > that!).[/color]

          I am sure, they will just turn off javascript and submit anyway, if
          you prevent this.

          Are you sure, you do not try to fix a non-problem? They obviously did
          not want to enter data in these fields. Would it make it any better
          for you, when you force them to enter "a" or "askdasjkh" just to
          fulfil the "non-empty" requirement?
          [color=blue]
          > Is there any way to plug up this hole that ain't
          > browser-specific?[/color]

          Any checks on browser supplied data has to be checked on the server
          side before usage anyway.

          Bye,
          Martin

          Comment

          • Michael Winter

            #6
            Re: HOW TO: *Not* Validate Spaces

            On 20 Dec 2004 17:36:35 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:

            [snip]
            [color=blue]
            > if (-1==document.frm Message.name.va lue.search(/\S/)) {[/color]

            Wouldn't the RegExp.prototyp e.test method be quicker and simpler?

            /* To the OP: if this called from the submit event, change the
            * call to
            *
            * <form ... onsubmit="retur n checkForm(this) ;">
            *
            * The form argument will now refer to the form and
            * "document.frmMe ssage" can be replaced with "form".
            * Alternatively, start your function with
            *
            * function CheckForm() {
            * var form = document.forms['frmMessage'];
            */
            function checkForm(form) {var elem = form.elements;
            if(!/\S/.test(elem['name'].value)) {
            /* The name control contains only whitespace, or it's blank. */
            }
            /* ... */
            }

            [snip]

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Lee

              #7
              Re: HOW TO: *Not* Validate Spaces

              Michael Winter said:[color=blue]
              >
              >On 20 Dec 2004 17:36:35 -0800, Lee <REM0VElbspamtr ap@cox.net> wrote:
              >
              >[snip]
              >[color=green]
              >> if (-1==document.frm Message.name.va lue.search(/\S/)) {[/color]
              >
              >Wouldn't the RegExp.prototyp e.test method be quicker and simpler?[/color]

              I believe (from old Netscape docs) that search() is at least as
              quick and I prefer its syntax.

              Comment

              • Dr John Stockton

                #8
                Re: HOW TO: *Not* Validate Spaces

                JRS: In article <10sevt2icu6pj4 6@corp.supernew s.com>, dated Mon, 20 Dec
                2004 17:43:07, seen in news:comp.lang. javascript, News Me
                <newsTWOme@paci fierDOTcom.?> posted :

                Please trim your quotes.
                [color=blue]
                >do a Google Groups search for "javascript trim", you'll get pages of
                >examples...[/color]

                Indeed. Many will be either wrong or inefficient.

                Trim functions are shown in the frequently-posted newsgroup FAQ, which
                sensible people read before posting questions here, let alone before
                posting answers. But it is not the best way here.

                Lee's method is (as customary) correct and almost certainly near-
                optimal, but I'd have put

                OK = /\S/.test(document. frmMessage.name .value)

                Our methods inevitably examine the whole of an empty string, but in the
                usual case of a string starting with a printing character they return
                immediately. Trim requires creating a new string for the result,
                whether or not either end of the input had blanks.

                If the OP wants his code to be legible - and he was asking for it to be
                read - he will in future indent lines and insert blank to show intended
                logical structure.

                For validation see <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Markus Ernst

                  #9
                  Re: HOW TO: *Not* Validate Spaces

                  Martin Bialasinski wrote:[color=blue]
                  > Wayfarer <wfping2004-x@yahoo.com> wrote:
                  >[color=green]
                  >> Some nefarious individuals have figured out a way to submit the
                  >> form by entering spaces in the required text fields (imagine
                  >> that!).[/color][/color]
                  [...][color=blue]
                  > Any checks on browser supplied data has to be checked on the server
                  > side before usage anyway.[/color]

                  I abandon extra checking for spaces, because this makes it easy to test the
                  server-side validation which trims the submitted values. So I can test this
                  without turning Javascript off, just by submitting the form with spaces
                  entered.

                  --
                  Markus


                  Comment

                  Working...