Validating form fields

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

    Validating form fields

    I want to validate that the data entered by a user is correct before they
    submit the form.

    How do I validate the following (or what functions/classes do I use):

    - Ensure a text field is not empty
    - Ensure that a text field contains only numeric values
    - Ensure that the contents of a text field is shorter than a certain length
    - Remove commas and other formatting from a field before submission

    TIA.


  • Daniel Kirsch

    #2
    Re: Validating form fields

    Davey wrote:[color=blue]
    > - Ensure a text field is not empty[/color]

    if (!yourfield.val ue)
    alert("empty")
    [color=blue]
    > - Ensure that a text field contains only numeric values[/color]

    if (yourfield.valu e.search(/\D/) != -1)
    alert("only numbers please")

    strip all non numeric values:
    var val = yourfield.value .replace(/\D/g,"");

    [color=blue]
    > - Ensure that the contents of a text field is shorter than a certain length[/color]

    if (!yourfield.val ue.length > 10)
    alert("to long")
    [color=blue]
    > - Remove commas and other formatting from a field before submission[/color]

    removing all of the following characters: ",.-+"
    var val = yourfield.value .replace(/[,.\-+]/g,"")
    // mask the minus as otherwise it will define a range

    Checkout any RegularExpressi on reference of your choice to learn more on
    this.

    HTH
    Daniel

    Comment

    • phal

      #3
      Re: Validating form fields


      You can use regular expression to do it, it very powerful for user
      input validation.

      Phal

      Comment

      • RobG

        #4
        Re: Validating form fields

        Daniel Kirsch wrote:[color=blue]
        > Davey wrote:
        >[/color]
        [...][color=blue][color=green]
        >> - Ensure that a text field contains only numeric values[/color]
        >
        >
        > if (yourfield.valu e.search(/\D/) != -1)
        > alert("only numbers please")[/color]


        if ( /\D/.test(yourfield .value ) )


        [...][color=blue][color=green]
        >> - Ensure that the contents of a text field is shorter than a certain
        >> length[/color]
        >
        >
        > if (!yourfield.val ue.length > 10)
        > alert("to long")[/color]

        The logic here is back-to-front, it will respond that the text is to
        long only if it is not greater than 10.

        if ( yourfield.value .length > 10 )
        alert("it's too long");


        [...]


        --
        Rob

        Comment

        • Daniel Kirsch

          #5
          Re: Validating form fields

          RobG wrote:[color=blue]
          > Daniel Kirsch wrote:[color=green]
          >> if (!yourfield.val ue.length > 10)
          >> alert("to long")[/color]
          >
          >
          > The logic here is back-to-front, it will respond that the text is to
          > long only if it is not greater than 10.[/color]

          Autsch. Another copy&paste bug.

          Thanks for correction
          Daniel

          Comment

          • Dr John Stockton

            #6
            Re: Validating form fields

            JRS: In article <4328031d$1_3 @x-privat.org>, dated Wed, 14 Sep 2005
            12:06:19, seen in news:microsoft. public.scriptin g.jscript, Davey
            <davey@hello.co m> posted :[color=blue]
            >I want to validate that the data entered by a user is correct before they
            >submit the form.[/color]

            You can only do that if you know what it should be, and in that case you
            don't need to bother with the form.
            [color=blue]
            >How do I validate the following (or what functions/classes do I use):
            >
            >- Ensure a text field is not empty
            >- Ensure that a text field contains only numeric values
            >- Ensure that the contents of a text field is shorter than a certain length
            >- Remove commas and other formatting from a field before submission[/color]

            For checks of compliance with a given format, which is not the same
            thing as before, see <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://www.jibbering.c om/faq/> JL/RC: FAQ of 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

            • Stephen Chalmers

              #7
              Re: Validating form fields

              Davey <davey@hello.co m> wrote in message news:4328031d$1 _3@x-privat.org...[color=blue]
              > I want to validate that the data entered by a user is correct before they
              > submit the form.
              >
              > How do I validate the following (or what functions/classes do I use):[/color]

              You have to invent your own and there are plenty of tutorials around.
              Does your form look anything like this?
              [color=blue]
              >
              > - Ensure a text field is not empty[/color]

              Is this a homework assignment.? <input type=text name='hw'>

              [color=blue]
              > - Ensure that a text field contains only numeric values[/color]

              How many days before it's due ? <input type=text name='late'>

              [color=blue]
              > - Ensure that the contents of a text field is shorter than a certain length[/color]

              How do you rate your percentage chances of passing the course? <input type=text name='noChance' >

              [color=blue]
              > - Remove commas and other formatting from a field before submission[/color]

              How much is this course worth to you? <input type=text name='bribe'>

              --
              S.C.


              Comment

              • Davey

                #8
                Re: Validating form fields

                "Stephen Chalmers" <ignoring@lycos .co.uk> wrote in message
                news:4328c350_2 @mk-nntp-2.news.uk.tisca li.com...[color=blue]
                > Davey <davey@hello.co m> wrote in message news:4328031d$1 _3@x-privat.org...[color=green]
                >> I want to validate that the data entered by a user is correct before they
                >> submit the form.
                >>
                >> How do I validate the following (or what functions/classes do I use):[/color]
                >
                > You have to invent your own and there are plenty of tutorials around.
                > Does your form look anything like this?
                >[color=green]
                >>
                >> - Ensure a text field is not empty[/color]
                >
                > Is this a homework assignment.? <input type=text name='hw'>
                >
                >[color=green]
                >> - Ensure that a text field contains only numeric values[/color]
                >
                > How many days before it's due ? <input type=text name='late'>
                >
                >[color=green]
                >> - Ensure that the contents of a text field is shorter than a certain
                >> length[/color]
                >
                > How do you rate your percentage chances of passing the course? <input
                > type=text name='noChance' >
                >
                >[color=green]
                >> - Remove commas and other formatting from a field before submission[/color]
                >
                > How much is this course worth to you? <input type=text name='bribe'>[/color]

                No it looks like this.

                Is Stephen Chalmers a wanker?
                <select name='ChalmersW anker'>
                <option value='1'>Yes</option>
                <option value='2'>Defin itely</option>
                </select>


                Comment

                • Jan-Christoph Ihrens

                  #9
                  Re: Validating form fields

                  "Davey" schrieb:
                  [color=blue]
                  > I want to validate that the data entered by a user is correct before
                  > they submit the form.[/color]

                  If there's a need to validate input for security reasons, do *not* do
                  this on the client side by using JavaScript. It's just too easy to get
                  around this. Of course there's nothing to say against an *additional*
                  client side check, but you shouldn't rely on that.

                  Greetings,
                  Jan

                  Comment

                  Working...