count a specific character in a string

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

    #1

    count a specific character in a string

    Hello,

    I am working on a form-validation script.
    There is a input-field where you input a float or integer numper (maximum
    price to output in database-query).

    But my Cold-Fusion Query generates a >error-message, if there is more than
    one dot in that value e.g. 15.60.50

    Is it possible to validate, that there is only one or no dot given as value
    for that field?

    i tried with:
    if (document.myFor m.myInputField. value.indexOf(" .")>1) {
    alert ("Please not more than one dot!!")
    return false
    }

    but that doesn't work

    Thanks for any help,
    Martin Nadoll


  • Silvio Bierman

    #2
    Re: count a specific character in a string


    "Martin Nadoll" <martin@nadoll. de> wrote in message
    news:c1g536$jmc $05$1@news.t-online.com...[color=blue]
    > Hello,
    >
    > I am working on a form-validation script.
    > There is a input-field where you input a float or integer numper (maximum
    > price to output in database-query).
    >
    > But my Cold-Fusion Query generates a >error-message, if there is more than
    > one dot in that value e.g. 15.60.50
    >
    > Is it possible to validate, that there is only one or no dot given as[/color]
    value[color=blue]
    > for that field?
    >
    > i tried with:
    > if (document.myFor m.myInputField. value.indexOf(" .")>1) {
    > alert ("Please not more than one dot!!")
    > return false
    > }
    >
    > but that doesn't work
    >
    > Thanks for any help,
    > Martin Nadoll
    >
    >[/color]

    Probably the most efficient way is looping over the characters and count for
    yourself. You could do a replaceAll with an empty string and compare
    lengths.

    Silvio Bierman


    Comment

    • Geoff Tucker

      #3
      Re: count a specific character in a string


      "Martin Nadoll" <martin@nadoll. de> wrote in message
      news:c1g536$jmc $05$1@news.t-online.com...[color=blue]
      > Hello,
      >
      > I am working on a form-validation script.
      > There is a input-field where you input a float or integer numper[/color]
      (maximum[color=blue]
      > price to output in database-query).
      >
      > But my Cold-Fusion Query generates a >error-message, if there is more[/color]
      than[color=blue]
      > one dot in that value e.g. 15.60.50
      >
      > Is it possible to validate, that there is only one or no dot given as[/color]
      value[color=blue]
      > for that field?
      >
      > i tried with:
      > if (document.myFor m.myInputField. value.indexOf(" .")>1) {
      > alert ("Please not more than one dot!!")
      > return false
      > }
      >
      > but that doesn't work
      >
      > Thanks for any help,
      > Martin Nadoll
      >[/color]

      var yourInput=docum ent.forms['myForm'].elements['myInputField'];

      if (yourInput.valu e.split(".").le ngth-1 > 1)
      {
      alert ("Please not more than one dot!!");
      return false;
      }

      Works for me
      Geoff


      Comment

      • Silvio Bierman

        #4
        Re: count a specific character in a string


        "Geoff Tucker" <geoff@tucker24 .fsnet.co.uk> wrote in message
        news:c1ghm3$vmj $1@newsg2.svr.p ol.co.uk...[color=blue]
        >
        > "Martin Nadoll" <martin@nadoll. de> wrote in message
        > news:c1g536$jmc $05$1@news.t-online.com...[color=green]
        > > Hello,
        > >
        > > I am working on a form-validation script.
        > > There is a input-field where you input a float or integer numper[/color]
        > (maximum[color=green]
        > > price to output in database-query).
        > >
        > > But my Cold-Fusion Query generates a >error-message, if there is more[/color]
        > than[color=green]
        > > one dot in that value e.g. 15.60.50
        > >
        > > Is it possible to validate, that there is only one or no dot given as[/color]
        > value[color=green]
        > > for that field?
        > >
        > > i tried with:
        > > if (document.myFor m.myInputField. value.indexOf(" .")>1) {
        > > alert ("Please not more than one dot!!")
        > > return false
        > > }
        > >
        > > but that doesn't work
        > >
        > > Thanks for any help,
        > > Martin Nadoll
        > >[/color]
        >
        > var yourInput=docum ent.forms['myForm'].elements['myInputField'];
        >
        > if (yourInput.valu e.split(".").le ngth-1 > 1)
        > {
        > alert ("Please not more than one dot!!");
        > return false;
        > }
        >
        > Works for me
        > Geoff
        >
        >[/color]

        Good idea but I think you should escape the "." because it has special
        meaning in a regexp.

        Silvio Bierman


        Comment

        • Michael Winter

          #5
          Re: count a specific character in a string

          On Tue, 24 Feb 2004 19:35:51 +0100, Martin Nadoll <martin@nadoll. de> wrote:
          [color=blue]
          > I am working on a form-validation script.
          > There is a input-field where you input a float or integer numper (maximum
          > price to output in database-query).
          >
          > But my Cold-Fusion Query generates a >error-message, if there is more
          > than one dot in that value e.g. 15.60.50
          >
          > Is it possible to validate, that there is only one or no dot given as
          > value for that field?[/color]

          Yes. String validation is best performed with regular expressions. The
          snipped below will check that the number in 'num' is either an integer, or
          a (simple) floating point representation:

          if( /^\d+(\.\d+)?$/.test( num )) {
          // num is valid
          } else {
          // num contains letters, symbols, or more than one dot (.)
          }

          The expression permits the following:

          - A string composed entirely of digits with a minimum of one digit (e.g.
          512)
          - A string that begins with at least one digit, followed by a single dot,
          and ending with at least one digit (e.g. 4.2 or 16.333)

          If users attempt to enter other valid number representations , such as 5e-2
          (0.05), they will fail. Any value that contains a letter or symbol will
          also fail.

          Don't forget: you should always perform validation on the server.
          Client-side validation is no substitute.

          Hope that helps,
          Mike

          --
          Michael Winter
          M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

          Comment

          • Randy Webb

            #6
            Re: count a specific character in a string

            Martin Nadoll wrote:[color=blue]
            > Hello,
            >
            > I am working on a form-validation script.
            > There is a input-field where you input a float or integer numper (maximum
            > price to output in database-query).
            >
            > But my Cold-Fusion Query generates a >error-message, if there is more than
            > one dot in that value e.g. 15.60.50
            >
            > Is it possible to validate, that there is only one or no dot given as value
            > for that field?
            >
            > i tried with:
            > if (document.myFor m.myInputField. value.indexOf(" .")>1) {
            > alert ("Please not more than one dot!!")
            > return false
            > }
            >
            > but that doesn't work[/color]

            myVar = document.myForm .myInputField.v alue.split('.') ;
            if (myVar.length>2 )
            {
            alert('You have entered more than one decimal point');
            }

            --
            Randy
            Chance Favors The Prepared Mind
            comp.lang.javas cript FAQ - http://jibbering.com/faq/

            Comment

            • Mick White

              #7
              Re: count a specific character in a string

              Martin Nadoll wrote:
              [color=blue]
              > i tried with:
              > if (document.myFor m.myInputField. value.indexOf(" .")>1) {
              > alert ("Please not more than one dot!!")
              > return false
              > }
              >
              > but that doesn't work
              >
              > Thanks for any help,
              > Martin Nadoll[/color]


              return !isNaN(document .myForm.myInput Field.value)
              Mick

              Comment

              • Dr John Stockton

                #8
                Re: count a specific character in a string

                JRS: In article <c1g536$jmc$05$ 1@news.t-online.com>, seen in
                news:comp.lang. javascript, Martin Nadoll <martin@nadoll. de> posted at
                Tue, 24 Feb 2004 19:35:51 :-
                [color=blue]
                >Is it possible to validate, that there is only one or no dot given as value
                >for that field?[/color]

                OK = !/\..*\./.test(F.X0.valu e)

                I've not tested for speed, but a RegExp scan ought to be reasonably
                efficient, and the method generates no additional objects.

                Most validation can be done in a similar manner, with the conditions for
                the test s supplied in an array of object literals - see <URL:http://www
                ..merlyn.demon. co.uk/js-valid.htm>.

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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

                • Dr John Stockton

                  #9
                  Re: count a specific character in a string

                  JRS: In article <opr3wfyoyl5vkl cq@news-text.blueyonder .co.uk>, seen in
                  news:comp.lang. javascript, Michael Winter <M.Winter@bluey onder.co.invali
                  d> posted at Wed, 25 Feb 2004 00:04:48 :-[color=blue]
                  >
                  >Don't forget: you should always perform validation on the server.
                  >Client-side validation is no substitute.[/color]

                  Half true; it applies to cases in which a form is submitted to a server
                  for subsequent processing, and acceptance of incorrect entries would be
                  against the interests of the page owner. It is a rather common case,
                  but it is not the only case. It is perfectly possible to serve a page
                  that does processing at the client on client-provided information,
                  presenting results to the client; in that case, only client-side
                  validation is possible, and if the user suborns the code that's his
                  problem.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                  <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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

                  • Dr John Stockton

                    #10
                    Re: count a specific character in a string

                    JRS: In article <406B0746.590E2 4A@agricoreunit ed.com>, seen in
                    news:comp.lang. javascript, Grant Wagner <gwagner@agrico reunited.com>
                    posted at Wed, 31 Mar 2004 17:58:31 :[color=blue]
                    >
                    >Not to mention that the amount of storage and bandwidth required by your
                    >posts admonishing people for including "superfluid " information in their
                    >attribution has probably far exceeded the amount of storage and bandwidth
                    >required by extra information in their attributions.[/color]

                    Moreover, the "authority" that Lahn quotes is just someone's Web page,
                    translated from German.

                    The true authoritative documents, the RFCs and the like, clearly
                    envisage without disfavour using a full attribution, and discuss how
                    different parts of such may be helpful in different circumstances.

                    One merit of such would apply in the case of the Lahn rant itself, or
                    rather that of the rest of the article. The article which he cites is
                    not now present in my newsbase; but, without a dated attribution, I
                    cannot see whether it is an aged article, part of a discussion here in
                    which there is no longer any interest, or whether the article is in a
                    newsgroup that I either do not take or retain for a shorter period.

                    Perhaps it has not occurred to Thomas Lahn that one day, when he grows
                    up, he may be seeking employment; and an employer, particularly of one
                    from a computer-related search, may well try an Internet search. Most
                    employers want people who can interact well with others; but not
                    monomaniacal despots /in posse/.

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME ©
                    Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
                    Web <URL:http://www.merlyn.demo n.co.uk/news-use.htm> : about usage of News.
                    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                    Comment

                    Working...