Textarea validate

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

    Textarea validate

    Hi

    really need some help with validating a textarea box. basically i have
    a text area box that must not have more the 910 characters entered and
    they must not be spread over more than 23 lines. I could check the
    total length but the problem is people could enter 1 character per
    line so you would end up with 910 lines

    can anyone help
  • Denis Perelyubskiy

    #2
    Re: Textarea validate

    Neil Shaw wrote:[color=blue]
    > Hi
    >
    > really need some help with validating a textarea box. basically i have
    > a text area box that must not have more the 910 characters entered and
    > they must not be spread over more than 23 lines. I could check the
    > total length but the problem is people could enter 1 character per
    > line so you would end up with 910 lines[/color]

    I don't know if there is a "preferred" way of doing things, but being a
    javascript neophyte I'd just count newlines - they should represent
    number of lines (minus one, since the last line may not have a break)

    Comment

    • RobG

      #3
      Re: Textarea validate

      Denis Perelyubskiy wrote:[color=blue]
      > Neil Shaw wrote:
      >[color=green]
      >> Hi
      >>
      >> really need some help with validating a textarea box. basically i have
      >> a text area box that must not have more the 910 characters entered and
      >> they must not be spread over more than 23 lines. I could check the
      >> total length but the problem is people could enter 1 character per
      >> line so you would end up with 910 lines[/color]
      >
      >
      > I don't know if there is a "preferred" way of doing things, but being a
      > javascript neophyte I'd just count newlines - they should represent
      > number of lines (minus one, since the last line may not have a break)[/color]

      Why do I get the feeling this is part of a JavaScript course
      assignment? The example below returns the number of lines entered by
      the user (essentially how many returns, plus one). It will not count
      lines created by browser wrapping inside the text area.

      If no newline characters have been entered, it returns "1" since it's
      all one line (even if wrapped by the browser).

      <form action="">
      <textarea name="ata"></textarea>
      <input type="button" value="Count lines"
      onclick="alert( escape(ata.valu e).split('%0A') .length);">
      </form>

      Rob.

      Comment

      • Dr John Stockton

        #4
        Re: Textarea validate

        JRS: In article <LSDcd.2167$aA. 71595@news.optu s.net.au>, dated Mon, 18
        Oct 2004 00:11:23, seen in news:comp.lang. javascript, RobG
        <rgqld@iinet.ne t.auau> posted :[color=blue]
        >
        > Why do I get the feeling this is part of a JavaScript course
        > assignment? The example below returns the number of lines entered by
        > the user (essentially how many returns, plus one). It will not count
        > lines created by browser wrapping inside the text area.
        >
        > If no newline characters have been entered, it returns "1" since it's
        > all one line (even if wrapped by the browser).
        >
        ><form action="">
        > <textarea name="ata"></textarea>
        > <input type="button" value="Count lines"
        > onclick="alert( escape(ata.valu e).split('%0A') .length);">
        ></form>[/color]


        Since newline can be considered as a line separator or a line
        terminator, a N b and a N b N might or might not be considered as having
        the same number of lines; and the line count could be measured in either
        manner. There is an essential ambiguity in the original question.

        Your method creates, AIUI, an array object plus a string object for each
        line, then abandons them.

        Something like the following creates only two temporary Objects :

        ans =
        ata.value.repla ce(/[^\r\n]/g, '').replace(/(\r\n|\r|\n)/g, '#').length

        N.B. The RegExps and their usage may not be optimised; there is an
        attempt to allow for all possible renditions of newline.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
        Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
        Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
        Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

        Comment

        Working...