a problem with text field verification

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

    a problem with text field verification

    I have to write a program to verify text field in HTML forms.
    So,I hane to verify a text field with lenth 10 & maxlenth 10.The
    entered text should be as follows.
    xxxxxxxxxy
    where xxxxxxxxx denotes a combination of numbers and y should be one
    of 'X','x','V'or 'v'.Also inputs such as 000000000v,0000 00000X are not
    possible.
  • HikksNotAtHome

    #2
    Re: a problem with text field verification

    In article <6d3c3fa8.03122 22032.5a718dc8@ posting.google. com>, dmag1@mail.com
    (gunawardana) writes:
    [color=blue]
    >I have to write a program to verify text field in HTML forms.
    >So,I hane to verify a text field with lenth 10 & maxlenth 10.The
    >entered text should be as follows.
    > xxxxxxxxxy
    > where xxxxxxxxx denotes a combination of numbers and y should be one
    >of 'X','x','V'or 'v'.Also inputs such as 000000000v,0000 00000X are not
    >possible.[/color]

    Try reading your schoolbook and doing your own homework?
    --
    Randy

    Comment

    • lallous

      #3
      Re: a problem with text field verification

      Hello,

      Validate the string as:

      str="012345678x ";
      var pat = new RegExp(/[0-9]{9}[xv]/i);
      if (pat.exec(str)= =null)
      alert('Invalid input!');

      --
      Elias

      "gunawardan a" <dmag1@mail.com > wrote in message
      news:6d3c3fa8.0 312222032.5a718 dc8@posting.goo gle.com...[color=blue]
      > I have to write a program to verify text field in HTML forms.
      > So,I hane to verify a text field with lenth 10 & maxlenth 10.The
      > entered text should be as follows.
      > xxxxxxxxxy
      > where xxxxxxxxx denotes a combination of numbers and y should be one
      > of 'X','x','V'or 'v'.Also inputs such as 000000000v,0000 00000X are not
      > possible.[/color]


      Comment

      • McKirahan

        #4
        Re: a problem with text field verification

        "gunawardan a" <dmag1@mail.com > wrote in message
        news:6d3c3fa8.0 312222032.5a718 dc8@posting.goo gle.com...[color=blue]
        > I have to write a program to verify text field in HTML forms.
        > So,I hane to verify a text field with lenth 10 & maxlenth 10.The
        > entered text should be as follows.
        > xxxxxxxxxy
        > where xxxxxxxxx denotes a combination of numbers and y should be one
        > of 'X','x','V'or 'v'.Also inputs such as 000000000v,0000 00000X are not
        > possible.[/color]


        I'm sure there's a Regular expression that does what you want with less
        coding but here's one solution; watch for word-wrap.


        <html>
        <head>
        <title>gunaward ana.htm</title>
        <script language="javas cript" type="text/javascript">
        <!--
        function check() {
        var form = document.forms[0];
        var data = form.Data.value ;
        if (data.length != 10) return;
        if (data.substr(0, 9) == "000000000" ) return;
        for (var i=0; i<9; i++) {
        if (data.charAt(i) < "0" || data.charAt(i) > "9") return;
        }
        if ("XxVv".indexOf (data.charAt(9) ) < 0) return;
        alert("OK!");
        }
        //-->
        </script>
        </head>
        <body>
        <form>
        <input type="text" name="Data" size="10" maxlength="10">
        <input type="button" value="Check" onclick="check( )">
        </form>
        </body>
        </html>


        Comment

        • Dr John Stockton

          #5
          Re: a problem with text field verification

          JRS: In article <6d3c3fa8.03122 22032.5a718dc8@ posting.google. com>, seen
          in news:comp.lang. javascript, gunawardana <dmag1@mail.com > posted at
          Mon, 22 Dec 2003 20:32:27 :-[color=blue]
          >I have to write a program to verify text field in HTML forms.
          >So,I hane to verify a text field with lenth 10 & maxlenth 10.The
          >entered text should be as follows.
          > xxxxxxxxxy
          > where xxxxxxxxx denotes a combination of numbers and y should be one
          >of 'X','x','V'or 'v'.Also inputs such as 000000000v,0000 00000X are not
          >possible.[/color]

          But what do you mean by "such as"? With leading zero? With all zeroes?

          There is an "or" facility in a RegExp, but not AFAIK an equivalent
          "and".

          Don't use a RegExp; use two, the second to deal with whatever "such as"
          means.

          OK = /^\d{9}(v|x)$/i.test(S) && /[1-9]/.test(S) // not 000000000
          OK = /^\[1-9]d{8}(v|x)$/i.test(S) // not leading zero

          See in <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://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

          • Lasse Reichstein Nielsen

            #6
            Re: a problem with text field verification

            Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
            [color=blue]
            > There is an "or" facility in a RegExp, but not AFAIK an equivalent
            > "and".[/color]

            Not directly. There could be, since there is nothing in the technology
            used that prohibits and "and" (and regular languages are closed under
            intersection).
            The closest you get is positive lookahead, i.e., to match five digits
            and at least one 4, you can write
            /^(?=\d{5})\d*4\ d*$/
            [color=blue]
            > Don't use a RegExp; use two,[/color]

            Agreed. Often, a very complex regular expressin can be written as
            two simple ones.

            Example: String contains n "a"'s and m "b"'s:

            Two regexps:
            /^[^a]*(a[^a]*){n}$/
            /^[^b]*(b[^b]*){m}$/

            I won't even begin to write a regexp for n and m with values much over 2.
            Try :)

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Eric Bohlman

              #7
              Re: a problem with text field verification

              Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in
              news:1xqvyoxp.f sf@hotpop.com:
              [color=blue]
              > Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
              >[color=green]
              >> There is an "or" facility in a RegExp, but not AFAIK an equivalent
              >> "and".[/color]
              >
              > Not directly. There could be, since there is nothing in the technology
              > used that prohibits and "and" (and regular languages are closed under
              > intersection).
              > The closest you get is positive lookahead, i.e., to match five digits
              > and at least one 4, you can write
              > /^(?=\d{5})\d*4\ d*$/[/color]

              Actually you can use positive lookahead to implement an arbitrary "and":
              /^(?=.*this)(?=. *that)/ (a trick introduced in the _Perl Cookbook_ and
              implemented in a Perl module of mine).

              However, doing two separate tests will usually be more efficient and the
              lookahead trick should probably be used only when the match parameters
              aren't known until runtime.

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: a problem with text field verification

                Eric Bohlman <ebohlman@earth link.net> writes:
                [color=blue]
                > Actually you can use positive lookahead to implement an arbitrary "and":
                > /^(?=.*this)(?=. *that)/ (a trick introduced in the _Perl Cookbook_ and
                > implemented in a Perl module of mine).[/color]

                The problem is that you can only do this efficiently at the end of a string.
                Compare this for "or':
                /z(aa|bbb)cd/
                If we had the hypothetical & operator, and wrote
                /z(.*this.*&.*th at.*)cd/
                then we wanted the part between "z" and "cd" to contain both "this"
                and "that".

                If you do that with lookahead, you need to be able to bound the search
                somehow, or the lookahead can test past the cd. As your example:
                /z(?=.*this)(.*t hat.*)cd/
                would incorrectly match
                "z that cd this"

                You need to ensure that the lookahead is only tested against the same
                string as the other argument to "and".


                You can do "the trick" and duplicate the continuation:
                /z(?=.*this.*cd) (.*that.*cd)/
                but even that can be broken by using more complex expressions. Take
                "all digits, and at least three 4's":

                /z(\d*&(.*4){3}. *)cd/
                Doing the trick here gives
                /z(?=\d*cd)(.*4) {3}.*cd/
                However, that also matches
                "z111cd444c d"

                Again, you have to build your RegExps so the lookahead is bounded,
                something that was not necessary with the hypothetical "&" operator.


                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: a problem with text field verification

                  lallous wrote:
                  [color=blue]
                  > str="012345678x ";
                  > var pat = new RegExp(/[0-9]{9}[xv]/i);[/color]

                  No. Either

                  var pat = /\d{9}[xv]/i;

                  or

                  var pat = new RegExp("\\d{9}[xv]", "i");
                  [color=blue]
                  > if (pat.exec(str)= =null)[/color]

                  if (! pat.test(str))
                  [color=blue]
                  > alert('Invalid input!');
                  >
                  > [Top post][/color]

                  Please do not do this, you are wasting
                  scarce and thus precious resources.


                  PointedEars

                  Comment

                  Working...