Validate form for 4-digit integer

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

    Validate form for 4-digit integer

    I have a text field in a form which I need to check to see that only a
    4-digit integer has been entered. The field has MAXLENGTH=4 and I'm
    using this to check for length:

    function checkpostcode(f orm) {
    var min=4;
    if (form.postcode. value.length < min) {
    alert("Please enter a 4-digit postcode");
    return false;
    }
    else return true;
    }

    That works fine but how can I extend that to check that the input
    consists only of four digits. Any help would be much appreciated.

    Bunyip

  • Lasse Reichstein Nielsen

    #2
    Re: Validate form for 4-digit integer

    Bunyip Bluegum <bunyip@austral ia> writes:
    [color=blue]
    > That works fine but how can I extend that to check that the input
    > consists only of four digits. Any help would be much appreciated.[/color]

    Sounds like a job for regular expressions;

    if (/^\d{4}$/.test(form.elem entsp['postcode'].value)) { ...

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • David W. Burhans

      #3
      Re: Validate form for 4-digit integer

      Bunyip Bluegum <bunyip@austral ia> wrote in message news:<krjcmv40q m47v3fq93gba4u5 feeacpjb9g@4ax. com>...[color=blue]
      > I have a text field in a form which I need to check to see that only a
      > 4-digit integer has been entered. The field has MAXLENGTH=4 and I'm
      > using this to check for length:
      >
      > function checkpostcode(f orm) {
      > var min=4;
      > if (form.postcode. value.length < min) {
      > alert("Please enter a 4-digit postcode");
      > return false;
      > }
      > else return true;
      > }
      >
      > That works fine but how can I extend that to check that the input
      > consists only of four digits. Any help would be much appreciated.
      >[/color]

      Use a regular expression to validate the format of a string. Here is an example:

      function checkpostcode(f orm)
      {
      var min = 4;
      var numberFormat = /^\d{4}$/;

      if (form.postcode. value.length == min)
      {
      if (numberFormat.t est(form.postco de.value)
      {
      return true;
      }
      else
      {
      alert("Please enter a 4-digit postcode");
      return false;
      }
      }
      else
      {
      alert("Please enter a 4-digit postcode");
      return false;
      }
      }

      Comment

      • Dr John Stockton

        #4
        Re: Validate form for 4-digit integer

        JRS: In article <ffb2fdbd.03091 60931.54026d8a@ posting.google. com>, seen
        in news:comp.lang. javascript, David W. Burhans
        <david_w_burhan s@yahoo.com> posted at Tue, 16 Sep 2003 10:31:58 :-
        [color=blue]
        >function checkpostcode(f orm)
        >{
        > var min = 4;
        > var numberFormat = /^\d{4}$/;
        >
        > if (form.postcode. value.length == min)
        > {
        > if (numberFormat.t est(form.postco de.value)[/color]


        There is no need to pre-test the length explicitly when the RegExp does
        it perfectly well.

        If the value entered was generally the wrong length, it is possible that
        insignificant execution time might be saved by the pre-test, at a cost
        of increased download time.

        If doing the pre-test, the RegExp test should be /\D/ since any non-
        digit is immediately fatal; this certainly saves insignificant time.

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

        • George M Jempty

          #5
          Re: Validate form for 4-digit integer

          Dr John Stockton wrote:
          [color=blue]
          > JRS: In article <ffb2fdbd.03091 60931.54026d8a@ posting.google. com>, seen
          > in news:comp.lang. javascript, David W. Burhans
          > <david_w_burhan s@yahoo.com> posted at Tue, 16 Sep 2003 10:31:58 :-
          >
          >[color=green]
          >>function checkpostcode(f orm)
          >>{
          >> var min = 4;
          >> var numberFormat = /^\d{4}$/;
          >>
          >> if (form.postcode. value.length == min)
          >> {
          >> if (numberFormat.t est(form.postco de.value)[/color]
          >
          >
          >
          > There is no need to pre-test the length explicitly when the RegExp does
          > it perfectly well.[/color]

          Additionally there is something else I believe is noteworthy. The OP
          speaks of a 4-digit postal code. Can the first digit be a zero? I'm
          guessing yes, and we are not going to be doing any arithmetic using the
          postal code.

          But if the first digit should NOT be a zero, then the regular expression
          would be:

          /^[1-9]\d{3}$/

          This of course would be particularly important if arithmetic were to be
          performed, because a leading zero would result in the number being
          evaluated as an octal.

          Comment

          • Dr John Stockton

            #6
            Re: Validate form for 4-digit integer

            JRS: In article <3f687b24$1_2@n ewsfeed.slurp.n et>, seen in
            news:comp.lang. javascript, George M Jempty <gjempty@interg ate.com>
            posted at Thu, 18 Sep 2003 09:18:08 :-[color=blue]
            >
            >But if the first digit should NOT be a zero, then the regular expression
            >would be:
            >
            >/^[1-9]\d{3}$/
            >
            >This of course would be particularly important if arithmetic were to be
            >performed, because a leading zero would result in the number being
            >evaluated as an octal.
            >[/color]

            Only by FAQ-ignorers. Never use parseInt without a second parameter,
            unless you want to allow octal conversion. AFAICS, all other methods of
            conversion allow only decimal & hexadecimal, except for parseFloat which
            only allows decimal.

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