test alphanumeric string

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

    test alphanumeric string

    I want the javascript to test an alphanumeric (a string contains
    alphabet or numbers only) string. Should I write a regular expression?
    What's the best way to do? please help. thanks
  • kaeli

    #2
    Re: test alphanumeric string

    In article <ba8a039e.04091 60817.7b0dfd76@ posting.google. com>,
    jrefactors@hotm ail.com enlightened us with...[color=blue]
    > I want the javascript to test an alphanumeric (a string contains
    > alphabet or numbers only) string. Should I write a regular expression?
    > What's the best way to do? please help. thanks
    >[/color]

    From my validation.js file...

    function checkAlphanum(s trObject)
    {
    /* Returns true if the field has all alphanumeric characters, false if
    not.
    You must pass in a input (text) object, not the value. */
    var re = /^[A-Za-z0-9]+$/;
    if (!strObject || isBlank(strObje ct)) return false;
    else return re.test(strObje ct.value);
    }

    function isBlank(strObje ct)
    {
    /* Returns true if the field is blank, false if not.
    You must pass in an input (text) object (not the value) */
    var re = /\S+/;

    if (!strObject) return true;
    return re.test(strObje ct.value));
    }

    HTH
    --
    --
    ~kaeli~
    If that phone was up your a$$, maybe you could drive a
    little better!



    Comment

    • Michael Winter

      #3
      Re: test alphanumeric string

      On Thu, 16 Sep 2004 13:01:01 -0500, kaeli <tiny_one@NOSPA M.comcast.net>
      wrote:

      [snip]
      [color=blue]
      > function checkAlphanum(s trObject)
      > {
      > /* Returns true if the field has all alphanumeric characters,
      > false if not.
      > You must pass in a input (text) object, not the value. */
      > var re = /^[A-Za-z0-9]+$/;
      > if (!strObject || isBlank(strObje ct)) return false;
      > else return re.test(strObje ct.value);
      > }[/color]

      Though the call to isBlank is redundant. As far as I can see, it would
      only be of significance if checkAlphanum allowed whitespace but you wanted
      to ensure that the value didn't contain only whitespace.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • kaeli

        #4
        Re: test alphanumeric string

        In article <MPG.1bb390d366 2847b98a134@nnt p.lucent.com>,
        tiny_one@NOSPAM .comcast.net enlightened us with...[color=blue]
        >
        > function isBlank(strObje ct)
        > {
        > /* Returns true if the field is blank, false if not.
        > You must pass in an input (text) object (not the value) */
        > var re = /\S+/;
        >
        > if (!strObject) return true;
        > return re.test(strObje ct.value));[/color]

        Whoops, this should be
        return !re.test(strObj ect.value));


        --
        --
        ~kaeli~
        The Bermuda Triangle got tired of warm weather. It moved to
        Finland. Now Santa Claus is missing.



        Comment

        • Dr John Stockton

          #5
          Re: test alphanumeric string

          JRS: In article <ba8a039e.04091 60817.7b0dfd76@ posting.google. com>,
          dated Thu, 16 Sep 2004 09:17:13, seen in news:comp.lang. javascript, Matt
          <jrefactors@hot mail.com> posted :[color=blue]
          >I want the javascript to test an alphanumeric (a string contains
          >alphabet or numbers only) string. Should I write a regular expression?[/color]

          Either that or copy one.
          [color=blue]
          >What's the best way to do? please help. thanks[/color]

          <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VP>, x1, adjust {6,10}.

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

          Working...