Plz urgent!string.match!

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

    #1

    Plz urgent!string.match!

    Hi,

    I want to validate whether a string is having only alphabets,
    numbers, '-' and '_'.

    Is there any way to do this like the one in Perl
    ( /^[0-9][A-Z][_][-]$/) ?

    Let me know the earliest?

  • oeyvind toft

    #2
    Re: Plz urgent!string.m atch!

    Something like this ?

    <script type="text/javascript">
    var strSample = 'abd_def-123_-';
    var reg = /^(\w|\d|-|_)+$/;
    var boolResult = reg.test(strSam ple);
    alert(boolResul t);
    </script>

    oeyvind

    --




    "learner" <v_nithya_v@yah oo.com> skrev i melding
    news:cgmgho$uh8 @odak26.prod.go ogle.com...[color=blue]
    > Hi,
    >
    > I want to validate whether a string is having only alphabets,
    > numbers, '-' and '_'.
    >
    > Is there any way to do this like the one in Perl
    > ( /^[0-9][A-Z][_][-]$/) ?
    >
    > Let me know the earliest?
    >[/color]


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Plz urgent!string.m atch!

      "learner" <v_nithya_v@yah oo.com> writes:
      [color=blue]
      > I want to validate whether a string is having only alphabets,
      > numbers, '-' and '_'.[/color]

      Letters and digits are easy, if you only want English language
      letters.
      [color=blue]
      > Is there any way to do this like the one in Perl
      > ( /^[0-9][A-Z][_][-]$/) ?[/color]

      That looks like a perfectly good Javascript regular expression, except
      that it probably doesn't do what you want.
      That regular expression matches only strings on the exact form:
      digit letter underscore minus

      Try
      /^[-0-9A-Z_]+$/g
      or the abbreviated form (\d matches digits, \w matches letters or underscore)
      /^[-\d\w]$/

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Dr John Stockton

        #4
        Re: Plz urgent!string.m atch!

        JRS: In article <cgmgho$uh8@oda k26.prod.google .com>, dated Thu, 26 Aug
        2004 22:24:08, seen in news:comp.lang. javascript, learner
        <v_nithya_v@yah oo.com> posted :
        [color=blue]
        >I want to validate whether a string is having only alphabets,
        >numbers, '-' and '_'.
        >
        >Is there any way to do this like the one in Perl
        >( /^[0-9][A-Z][_][-]$/) ?
        >
        > Let me know the earliest?[/color]
        ^ --- please.

        That expression does not do what you want, as has been explained.

        Rather than testing whether the whole string contains only those
        characters with /^[ characters ]*$/ it may be quicker to see if it
        contains any character other than those : /[^ characters ]/ .

        The latter process should stop at the first alien; the former, depending
        on implementation, probably scans the whole string even if it is faulty.

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