form validation: postal / zip codes

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

    #1

    form validation: postal / zip codes

    I used some of the excellent resources from DITHERING.COM for help in my
    groveling newbie attempts to cough up working form validation....

    I cut and pasted bits of code to check USA ZIP codes and CANADIAN POSTAL
    codes, and merged them into one function that I called validCode. The
    <form> tag has an onSubmit call to a general form-checker that works fine to
    make sure all fields are filled. But within the form is a ZIP/POSTAL CODE
    field, where the onBlur calls the function below: "
    onBlur="validCo de();".... The form-checking works fine, but when the
    "onBlur" is engaged, it gives me an "object expected" error where I'm
    defining the "var zip".

    If any of you seasoned warriors could lend me a neuron or 2, I owe you a
    cappuchino or more...

    =============== ==============

    function validCode(zip) {
    var zip = removeSpaces(zi p);
    if (!(zip.length == 5 || zip.length == 9 || zip.length == 10)) return
    false;
    if ((zip.length == 5 || zip.length == 9) && !isNumeric(zip) ) return
    false;
    if (zip.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/)
    == -1) return false;
    return true;
    // Check that a Canadian postal code is valid
    if (zip.search) {
    zip = removeSpaces(zi p);
    if (zip.length == 6 && zip.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/)
    != -1) return true;
    else if (zip.length == 7 &&
    zip.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
    else return false;
    }
    return true;
    }
    // ---> END
    </script>
    =============== ==============


  • Randy Webb

    #2
    Re: form validation: postal / zip codes

    Axel Foley wrote:[color=blue]
    > I used some of the excellent resources from DITHERING.COM for help in my
    > groveling newbie attempts to cough up working form validation....
    >
    > I cut and pasted bits of code to check USA ZIP codes and CANADIAN POSTAL
    > codes, and merged them into one function that I called validCode. The
    > <form> tag has an onSubmit call to a general form-checker that works fine to
    > make sure all fields are filled. But within the form is a ZIP/POSTAL CODE
    > field, where the onBlur calls the function below: "
    > onBlur="validCo de();".... The form-checking works fine, but when the
    > "onBlur" is engaged, it gives me an "object expected" error where I'm
    > defining the "var zip".
    >
    > If any of you seasoned warriors could lend me a neuron or 2, I owe you a
    > cappuchino or more...
    >
    > =============== ==============
    >
    > function validCode(zip) {
    > var zip = removeSpaces(zi p);[/color]

    You are calling onBlur="validCo de();"
    but it needs to be:
    onBlur="validCo de(this.value); "

    The function is expecting a parameter, tries to pass a parameter, and
    you aren't passing the parameter.

    Also, don't use onBlur to check it, use onChange instead. Then, it only
    gets checked when its actually been changed. Tabbing through the form
    will cause the onBlur to be needlessly fired.

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • Dr John Stockton

      #3
      Re: form validation: postal / zip codes

      JRS: In article <vgRCc.14444$HS 3.10438@edtnps8 4>, seen in
      news:comp.lang. javascript, Axel Foley <devmoaner@sync hkat.com> posted at
      Fri, 25 Jun 2004 08:21:47 :
      [color=blue]
      >I used some of the excellent resources from DITHERING.COM for help in my[/color]
      ?????????[color=blue]
      >groveling newbie attempts to cough up working form validation....[/color]


      US codes look like 12345, 123456789, 12345-6789
      CA codes look like B2B2B2, B2B 2B2

      Consider

      function validCode(zip) { var OK // or \s? v
      OK = /^(\d{5}|\d{9}|\ d{5}-\d{4}|[a-z]\d[a-z]\s*\d[a-z]\d)$/i.test(zip)
      return OK }
      [color=blue]
      >function validCode(zip) {
      > var zip = removeSpaces(zi p);
      > ...
      >// Check that a Canadian postal code is valid
      > if (zip.search) {
      > zip = removeSpaces(zi p);[/color]

      Did you not already remove them?

      IMHO, removing too many spaces is a mistake; for example, Ol 944 5683
      has 9 digits, but is much more likely to be an old munged UK phone
      number than a new US Zip.

      RegExp tested in <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#RT>.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> JL / RC : 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

      Working...