Form Validation Question.

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

    Form Validation Question.

    I've already created a simple method of ensuring that all form feilds
    are filled out before the form is submitted to an ASP page for records
    to be added to the data base.

    (Sorry about the formating, my newsreader may make it a mess!)

    <script language="javas cript">
    <!--
    function Check(form)
    {
    if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
    {
    alert("Please include an ID that is 8 characters");
    form.PersonID.f ocus();
    return false;
    }

    //There are more checking feilds here as well for phone number etc...

    {
    return true;
    }
    }
    //-->
    </script>

    At the moment, that code above just makes sure that the user has entered
    at least 8 characters/numbers.

    Now what I want to do is make sure that in the PersonID feild that the
    user enters only an id that begins with the letter "p" (lowercase only)
    followed only by any 7 numbers.

    ie. p1234567, p7654321, p2468135 etc...

    How do I do this?

    Cheers.
  • Erwin Moller

    #2
    Re: Form Validation Question.

    Drew wrote:
    [color=blue]
    > I've already created a simple method of ensuring that all form feilds
    > are filled out before the form is submitted to an ASP page for records
    > to be added to the data base.
    >
    > (Sorry about the formating, my newsreader may make it a mess!)
    >
    > <script language="javas cript">
    > <!--
    > function Check(form)
    > {
    > if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
    > {
    > alert("Please include an ID that is 8 characters");
    > form.PersonID.f ocus();
    > return false;
    > }
    >
    > //There are more checking feilds here as well for phone number etc...
    >
    > {
    > return true;
    > }
    > }
    > //-->
    > </script>
    >
    > At the moment, that code above just makes sure that the user has entered
    > at least 8 characters/numbers.
    >
    > Now what I want to do is make sure that in the PersonID feild that the
    > user enters only an id that begins with the letter "p" (lowercase only)
    > followed only by any 7 numbers.
    >
    > ie. p1234567, p7654321, p2468135 etc...
    >
    > How do I do this?
    >
    > Cheers.[/color]

    use String.substrin g(from, to)
    so:
    testvar = "Hello!";
    firstletter = testvar.substri ng(0,1);


    Regards,
    Erwin Moller

    Comment

    • Grant Wagner

      #3
      Re: Form Validation Question.

      Erwin Moller wrote:
      [color=blue]
      > Drew wrote:
      >[color=green]
      > > I've already created a simple method of ensuring that all form feilds
      > > are filled out before the form is submitted to an ASP page for records
      > > to be added to the data base.
      > >
      > > (Sorry about the formating, my newsreader may make it a mess!)
      > >
      > > <script language="javas cript">
      > > <!--
      > > function Check(form)
      > > {
      > > if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)
      > > {
      > > alert("Please include an ID that is 8 characters");
      > > form.PersonID.f ocus();
      > > return false;
      > > }
      > >
      > > //There are more checking feilds here as well for phone number etc...
      > >
      > > {
      > > return true;
      > > }
      > > }
      > > //-->
      > > </script>
      > >
      > > At the moment, that code above just makes sure that the user has entered
      > > at least 8 characters/numbers.
      > >
      > > Now what I want to do is make sure that in the PersonID feild that the
      > > user enters only an id that begins with the letter "p" (lowercase only)
      > > followed only by any 7 numbers.
      > >
      > > ie. p1234567, p7654321, p2468135 etc...
      > >
      > > How do I do this?
      > >
      > > Cheers.[/color]
      >
      > use String.substrin g(from, to)
      > so:
      > testvar = "Hello!";
      > firstletter = testvar.substri ng(0,1);
      >
      > Regards,
      > Erwin Moller[/color]

      var s = 'p12345678';
      alert(s.charAt( 0));

      However, your validation as it stands would allow me to enter " " and it
      would be valid. Even if you add validation to ensure the first letter is "p" to
      what you already have:

      if (form.PersonID. value == "" || form.PersonID.v alue.length != 8 ||
      form.PersonID.v alue.charAt(0) != "p")

      I could still enter "p ".

      You may want to consider implementing the trim() functionality available from
      this newsgroup's FAQ <url: http://jibbering.com/faq/#FAQ4_16 /> to help with
      your validation:

      var personId = form.PersonID.v alue.trim();
      if (personId == "" || personId != 8 || personId.charAt (0) != "p")

      or, you could perform your validation with regular expressions, which will
      ensure an exact match:

      if (!/p\d{7}/.test(form.Pers onID.value)) {
      // PersonID isn't valid
      }

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      • Mick White

        #4
        Re: Form Validation Question.

        Drew wrote:[color=blue]
        > Now what I want to do is make sure that in the PersonID feild that the
        > user enters only an id that begins with the letter "p" (lowercase only)
        > followed only by any 7 numbers.
        >
        > ie. p1234567, p7654321, p2468135 etc...
        >
        > How do I do this?[/color]


        function Check(form){
        if (!/^p{\d}7$/.test(form.Pers onID.value) ) {
        alert("Please include an ID that is p followed by seven numbers");
        form.PersonID.f ocus();
        return false;
        }

        ....
        }
        </script>

        Mick

        Comment

        • Dr John Stockton

          #5
          Re: Form Validation Question.

          JRS: In article <40b4179f$0$316 78$afc38c87@new s.optusnet.com. au>, seen
          in news:comp.lang. javascript, Drew <drew@fake.co m> posted at Wed, 26 May
          2004 14:05:56 :[color=blue]
          >I've already created a simple method of ensuring that all form feilds
          >are filled out before the form is submitted to an ASP page for records
          >to be added to the data base.[/color]
          [color=blue]
          ><script language="javas cript">[/color]
          // deprecated
          [color=blue]
          >function Check(form)
          >{
          > if (form.PersonID. value == "" || form.PersonID.v alue.length != 8)[/color]

          Since you test the length to be 8, ISTM unnecessary to test the empty
          case first.
          [color=blue]
          > {
          > alert("Please include an ID that is 8 characters");
          > form.PersonID.f ocus();
          > return false;
          > }
          >
          > //There are more checking feilds here as well for phone number etc...
          >
          > {
          > return true;
          > }
          >}[/color]

          [color=blue]
          >At the moment, that code above just makes sure that the user has entered
          >at least 8 characters/numbers.[/color]

          Exactly 8, it seems.
          [color=blue]
          >Now what I want to do is make sure that in the PersonID feild that the
          >user enters only an id that begins with the letter "p" (lowercase only)
          >followed only by any 7 numbers.[/color]

          You mean one number of seven (decimal) digits.
          [color=blue]
          >ie. p1234567, p7654321, p2468135 etc...
          >
          >How do I do this?[/color]

          See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm>; use such as
          OK = /^p\d{7}$/.test(form.Pers onID.value)
          if (!OK) alert("Aaargh!" )
          return OK

          That page also has parameter-driven validation, which enables brief
          expression of multiple tests on multiple fields.

          --
          © 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

          • Drew

            #6
            Re: Form Validation Question.

            Thanks for the help guys!

            Comment

            • Erwin Moller

              #7
              Re: Form Validation Question.

              Drew wrote:
              [color=blue]
              > Thanks for the help guys![/color]

              You are welcome.

              You have now three ways of doing it: substring, charAt, and a regular
              expression. Make your pick.

              Please bookmark this:


              and check it before you have post a javascriptquest ion.
              I estimate 90% of the questions asked here is answered there.
              Hence the name FAQ. :P

              Regards,
              Erwin Moller

              Comment

              Working...