Validating

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

    Validating

    Hi,

    Is it possible to validate the 1st 2 characters in a text box as letter,
    then the following 5 characters as numbers and if the input is incorrect for
    it to display an alert?
    It seems to be checking the entire field as opposed to the first 2
    characters being letters & the following 5 being numbers.

    The code I am using at the moment is:
    function validateStudent Number(studentN umber)
    {
    if (isBlank(studen tNumber)) // blank?
    {
    alert("Enter your student number please in the format of 2 uppercase
    letters followed by 5 numbers")
    return false
    }
    for (var i = 0; i < studentNumber.l ength; i++) // letters?
    {
    var testChar = studentNumber.c harAt(i)
    if (testChar < "A" || testChar > "Z")
    {
    alert("Enter your student number please in the format of 2 uppercase
    letters followed by 5 numbers")
    return false
    }
    for (var i = 0; i < studentNumber.l ength; i++) // numeric?
    {
    var testChar = studentNumber.c harAt(i)
    if (testChar < "0" || testChar > "9")
    }
    alert("Enter your student number please in the format of 2 uppercase
    letters followed by 5 numbers")
    return false
    }


    Many Thanks

    Mark




  • Thomas 'PointedEars' Lahn

    #2
    Re: Validating

    Mark wrote:
    [color=blue]
    > Is it possible to validate the 1st 2 characters in a text box as letter,
    > then the following 5 characters as numbers and if the input is incorrect for
    > it to display an alert?^[/color]

    Reads familiar to me, have you searched with Google Groups before posting?
    [color=blue]
    > It seems to be checking the entire field as opposed to the first 2
    > characters being letters & the following 5 being numbers.
    >
    > The code I am using at the moment is:
    > [much too complicated :-)][/color]

    Use Regular Expressions (RegExp) instead:

    var s = referenceToText Box.value;
    if (! /^\w{2}[0-9]{5}/.test(s))
    alert("BOO!");


    PointedEars

    Comment

    • Mark

      #3
      Re: Validating

      Thanks for that!

      Am I implimenting this into my code right as I keep getting an Object
      expected error:

      function validateNumber( Number)
      {
      if (isBlank(Number )) // blank?
      {
      alert("Enter your number please in the format of 2 uppercase
      letters followed by 5 numbers")
      return false
      }
      var s = Number.value;
      if (! /^\w{2}[0-9]{5}/.test(s))
      {
      alert("Enter your number please in the format of 2 uppercase
      letters followed by 5 numbers");
      return false
      }
      return true
      }

      Thanks for your help once again, sorry if the question seen dense but im new
      to Javascripting

      Mark

      "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
      news:boij0s$1ep abl$1@ID-107532.news.uni-berlin.de...[color=blue]
      > Mark wrote:
      >[color=green]
      > > Is it possible to validate the 1st 2 characters in a text box as letter,
      > > then the following 5 characters as numbers and if the input is incorrect[/color][/color]
      for[color=blue][color=green]
      > > it to display an alert?^[/color]
      >
      > Reads familiar to me, have you searched with Google Groups before posting?
      >[color=green]
      > > It seems to be checking the entire field as opposed to the first 2
      > > characters being letters & the following 5 being numbers.
      > >
      > > The code I am using at the moment is:
      > > [much too complicated :-)][/color]
      >
      > Use Regular Expressions (RegExp) instead:
      >
      > var s = referenceToText Box.value;
      > if (! /^\w{2}[0-9]{5}/.test(s))
      > alert("BOO!");
      >
      >
      > PointedEars
      >[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Validating

        Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
        [color=blue]
        > Mark wrote:
        >[color=green]
        > > Is it possible to validate the 1st 2 characters in a text box as letter,
        > > then the following 5 characters as numbers and if the input is incorrect for
        > > it to display an alert?^[/color]
        >
        > Use Regular Expressions (RegExp) instead:
        >
        > var s = referenceToText Box.value;
        > if (! /^\w{2}[0-9]{5}/.test(s))[/color]

        That actually fails the specification, since Word Characters (\w)
        includes digits (which can be abbreviated as \d).

        Unless the original poster wants international characters in the first
        two positions, he can use
        if (! (/^[a-z]{2}\d{5}/g).test(s))

        He didn't say, but my guess is that there can be nothing after the
        five digits, so the regular expression should probably have a "$"
        before the last "/".

        /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

        • Lasse Reichstein Nielsen

          #5
          Re: Validating

          "Mark" <not@chance.com > writes:
          [color=blue]
          > function validateNumber( Number)[/color]

          The Number argument is a string containing the value of the input element.
          Correct?
          [color=blue]
          > var s = Number.value;[/color]

          That means that you shouldn't try to get the value again. Strings don't
          have a property called "value". Just
          var s = Number;
          (or just use Number in the next line)
          [color=blue]
          > if (! /^\w{2}[0-9]{5}/.test(s))[/color]

          You probably want
          if (! (/^[A-Z]{2}\d{5}/).test(Number))

          It only allows uppercase English letters.
          This test also fails if the string is empty, so you don't need
          the isBlank test.
          [color=blue]
          > {
          > alert("Enter your number please in the format of 2 uppercase
          > letters followed by 5 numbers");[/color]

          Use "digits", not "numbers". Five numbers could be 42, 37, 1594323, pi
          and -24 :)

          [color=blue]
          > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
          > news:boij0s$1ep abl$1@ID-107532.news.uni-berlin.de...[/color]

          Please don't quote the entire previous message if you don't reply
          directly to it. Trim your quotes, and preferably reply below quotes of
          the parts you reply to (like I do :).

          /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

          • Mark

            #6
            Re: Validating

            Thanks for your help guys, got it working a treat!!


            "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
            news:k76b6pah.f sf@hotpop.com.. .[color=blue]
            > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
            >[color=green]
            > > Mark wrote:
            > >[color=darkred]
            > > > Is it possible to validate the 1st 2 characters in a text box as[/color][/color][/color]
            letter,[color=blue][color=green][color=darkred]
            > > > then the following 5 characters as numbers and if the input is[/color][/color][/color]
            incorrect for[color=blue][color=green][color=darkred]
            > > > it to display an alert?^[/color]
            > >
            > > Use Regular Expressions (RegExp) instead:
            > >
            > > var s = referenceToText Box.value;
            > > if (! /^\w{2}[0-9]{5}/.test(s))[/color]
            >
            > That actually fails the specification, since Word Characters (\w)
            > includes digits (which can be abbreviated as \d).
            >
            > Unless the original poster wants international characters in the first
            > two positions, he can use
            > if (! (/^[a-z]{2}\d{5}/g).test(s))
            >
            > He didn't say, but my guess is that there can be nothing after the
            > five digits, so the regular expression should probably have a "$"
            > before the last "/".
            >
            > /L
            > --
            > Lasse Reichstein Nielsen - lrn@hotpop.com
            > DHTML Death Colors:[/color]
            <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
            > 'Faith without judgement merely degrades the spirit divine.'[/color]


            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Validating

              Lasse Reichstein Nielsen wrote:[color=blue]
              > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:[color=green]
              >> Mark wrote:[color=darkred]
              >> > Is it possible to validate the 1st 2 characters in a text box as letter,
              >> > then the following 5 characters as numbers and if the input is incorrect for
              >> > it to display an alert?^[/color]
              >>
              >> Use Regular Expressions (RegExp) instead:
              >>
              >> var s = referenceToText Box.value;
              >> if (! /^\w{2}[0-9]{5}/.test(s))[/color]
              >
              > That actually fails the specification, since Word Characters (\w)
              > includes digits (which can be abbreviated as \d).[/color]

              ACK, my bad.
              [color=blue]
              > Unless the original poster wants international characters in the first
              > two positions, he can use
              > if (! (/^[a-z]{2}\d{5}/g).test(s))[/color]

              `/g' is inefficient here and the parantheses around the RegExp are not
              required. But `/i' would be useful here because `letters' includes
              lowercase *and* uppercase letters. Did you mix up both flags?


              PointedEars

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Validating

                Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                [color=blue]
                > `/g' is inefficient here and the parantheses around the RegExp are not
                > required. But `/i' would be useful here because `letters' includes
                > lowercase *and* uppercase letters. Did you mix up both flags?[/color]

                Yep. I meant "i". :)
                /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

                • Dr John Stockton

                  #9
                  Re: Validating

                  JRS: In article <bom1ec$1f0n81$ 1@ID-107532.news.uni-berlin.de>, seen in
                  news:comp.lang. javascript, Thomas 'PointedEars' Lahn
                  <PointedEars@we b.de> posted at Sun, 9 Nov 2003 19:37:35 :-
                  [color=blue]
                  > But `/i' would be useful here because `letters' includes
                  >lowercase *and* uppercase letters.[/color]

                  Indeed it does.

                  But the error messages presented on the code in the first article in the
                  thread included two instances of

                  alert("Enter your student number please in the format of 2 uppercase
                  letters followed by 5 numbers")

                  which implies that the letters should be specified as A-Z, without i
                  modifier. The OP is apparently within the UK, so there is probably no
                  need to consider upper-case letters outside that range, such as Æ Ð Ö.

                  --
                  © 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> JS maths, dates, sources.
                  <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

                  Comment

                  Working...