Regular expression for required alpha and numeric characters

Discussion in 'Javascript' started by .Net Sports, Apr 18, 2007.

  1. .Net Sports

    .Net Sports Guest

    I am checking for text input on a form validation in javascript that
    required at least one numeric character along with any number of alpha
    characters for a given input text box. The below is a var declare that
    does a method to check for alpha, or numeric, or - _ characters

    var charpos = objValue.value.search("[^A-Za-z0-9\-_]");

    but doing:

    var charpos = objValue.value.search("[^A-Za-z0-9]");

    ....doesnt work.

    ????
    netsports
     
    .Net Sports, Apr 18, 2007
    #1
    1. Advertising

  2. .Net Sports

    shimmyshack Guest

    On Apr 18, 10:13 pm, ".Net Sports" <> wrote:
    > I am checking for text input on a form validation in javascript that
    > required at least one numeric character along with any number of alpha
    > characters for a given input text box. The below is a var declare that
    > does a method to check for alpha, or numeric, or - _ characters
    >
    > var charpos = objValue.value.search("[^A-Za-z0-9\-_]");
    >
    > but doing:
    >
    > var charpos = objValue.value.search("[^A-Za-z0-9]");
    >
    > ...doesnt work.
    >
    > ????
    > netsports


    var value = 'hi there';
    var RegExp = /^[A-Za-z0-9\-_]{6,16}$/;

    if( RegExp.test(value) )
    {
    alert( 'yipee' );
    }

    of course best to limit the max number of chars as well as the min,
    here it's 6 to 16.
     
    shimmyshack, Apr 18, 2007
    #2
    1. Advertising

  3. .Net Sports

    .Net Sports Guest

    Thanks for reply, but what I need is to have users enter a proposed
    password, whereas the password has to have at least one numeric
    character, and yes your idea of between 6 and 16 character range is
    acceptable, but what I have is a switch-case routine, whereas one of
    the case statements as shown below checks for just alpha character :

    case "alpha":
    {
    var charpos = objValue.value.search("[^A-Za-z]");
    if(objValue.value.length > 0 && charpos >= 0)
    {
    if(!strError || strError.length ==0)
    {
    strError = objValue.name+": Only alphabetic
    characters allowed ";
    }//if
    alert(strError + "\n [Error character position " +
    eval(charpos+1)+"]");
    return false;
    }//if
    break;
    }

    ....and i'm having trouble figuring how to add the added requirement of
    "at least one numeric character" constraint.
    thanx
    Netsports

    On Apr 18, 3:20 pm, shimmyshack <> wrote:
    > On Apr 18, 10:13 pm, ".Net Sports" <> wrote:
    >
    > > I am checking for text input on a form validation in javascript that
    > > required at least one numeric character along with any number of alpha
    > > characters for a given input text box. The below is a var declare that
    > > does a method to check for alpha, or numeric, or - _ characters

    >
    > > var charpos = objValue.value.search("[^A-Za-z0-9\-_]");

    >
    > > but doing:

    >
    > > var charpos = objValue.value.search("[^A-Za-z0-9]");

    >
    > > ...doesnt work.

    >
    > > ????



















    > > netsports

    >
    > var value = 'hi there';
    > var RegExp = /^[A-Za-z0-9\-_]{6,16}$/;
    >
    > if( RegExp.test(value) )
    > {
    > alert( 'yipee' );
    >
    > }
    >
    > of course best to limit the max number of chars as well as the min,
    > here it's 6 to 16.
     
    .Net Sports, Apr 19, 2007
    #3
  4. .Net Sports

    shimmyshack Guest

    On Apr 19, 5:28 am, Good Man <> wrote:
    > ".Net Sports" <> wrote in news:1176950921.045685.277030
    > @o5g2000hsb.googlegroups.com:
    >
    > > eval(charpos+1)+"]");
    > > return false;
    > > }//if
    > > break;
    > > }

    >
    > > ...and i'm having trouble figuring how to add the added requirement of
    > > "at least one numeric character" constraint.


    oops just remove ^ and $

    var value = 'hi th4ere';
    var RegExp = /[A-Za-z0-9]{6,16}/;

    if( RegExp.test(value) )
    {
    alert( 'yipee' );

    }

    this /does/ impose the requirement you are after.
    >
    > you seem to have more than one problem going on... using 'eval' is highly,
    > highly discouraged and quite possibly dangerous.
     
    shimmyshack, Apr 19, 2007
    #4
  5. .Net Sports

    Lee Guest

    shimmyshack said:
    >
    >On Apr 19, 5:28 am, Good Man <> wrote:
    >> ".Net Sports" <> wrote in news:1176950921.045685.277030
    >> @o5g2000hsb.googlegroups.com:
    >>
    >> > eval(charpos+1)+"]");
    >> > return false;
    >> > }//if
    >> > break;
    >> > }

    >>
    >> > ...and i'm having trouble figuring how to add the added requirement of
    >> > "at least one numeric character" constraint.

    >
    >oops just remove ^ and $
    >
    >var value = 'hi th4ere';
    >var RegExp = /[A-Za-z0-9]{6,16}/;
    >
    >if( RegExp.test(value) )
    >{
    > alert( 'yipee' );
    >
    >}
    >
    >this /does/ impose the requirement you are after.


    How does that require at least one numeric character?


    --
     
    Lee, Apr 19, 2007
    #5
  6. In comp.lang.javascript message <
    glegroups.com>, Thu, 19 Apr 2007 01:50:27, shimmyshack
    <> posted:

    >> > ...and i'm having trouble figuring how to add the added requirement of
    >> > "at least one numeric character" constraint.


    >var value = 'hi th4ere';
    >var RegExp = /[A-Za-z0-9]{6,16}/;
    >
    >if( RegExp.test(value) )
    >{
    > alert( 'yipee' );
    >
    >}
    >
    >this /does/ impose the requirement you are after.



    Does it enforce at least one character of each type? I think not.

    One could do a \d test, and an [a-z] test, and AND the results; but
    seeing that the OP seems only to allow alphanumerics then there must be
    at least one of digit-letter & letter-digit present, so :

    OK = /\d[a-z]|[a-z]\d/i.test(F.X0.value)

    If _ is allowed, change [a-z] to \w and omit i. Test it more.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Apr 19, 2007
    #6
  7. .Net Sports

    shimmyshack Guest

    On Apr 19, 9:50 am, shimmyshack <> wrote:
    > On Apr 19, 5:28 am, Good Man <> wrote:
    >
    > > ".Net Sports" <> wrote in news:1176950921.045685.277030
    > > @o5g2000hsb.googlegroups.com:

    >
    > > > eval(charpos+1)+"]");
    > > > return false;
    > > > }//if
    > > > break;
    > > > }

    >
    > > > ...and i'm having trouble figuring how to add the added requirement of
    > > > "at least one numeric character" constraint.

    >
    > oops just remove ^ and $
    >
    > var value = 'hi th4ere';
    > var RegExp = /[A-Za-z0-9]{6,16}/;
    >
    > if( RegExp.test(value) )
    > {
    > alert( 'yipee' );
    >
    > }
    >
    > this /does/ impose the requirement you are after.
    >
    >
    >
    > > you seem to have more than one problem going on... using 'eval' is highly,
    > > highly discouraged and quite possibly dangerous.


    well I see what you are after now, sorry for the confusion - I think
    you/ were clear - you must use something like

    var value = 'hith9ere';
    var RegExp = /^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$/
    //or
    //var RegExp = /^[a-z0-9]*[0-9]+[a-z0-9]*$/i
    //or
    //var RegExp = /^\w*\d+\w*$/


    if( RegExp.test(value) )
    {
    //ok
    }

    of course this allows 0 or more matches in the ranges a-z A-z 0-9
    before at least one 0-9 followed by 0 or more matches from the larger
    range again.
    you could use the i modifier to reduce the complexity of the reg exp
    slightly, or in further by using the \w* and \d+ character escapes
    although \w includes the underscore character which you might not want
    to allow. The 0-9 is repeated in the ranges because you might want
    characters from the 0-9 to be repeated non-consecutively. sorry for
    being needlessly sure of myself before!
     
    shimmyshack, Apr 19, 2007
    #7
  8. .Net Sports

    shimmyshack Guest

    On Apr 19, 6:24 pm, shimmyshack <> wrote:
    > On Apr 19, 9:50 am, shimmyshack <> wrote:
    >
    >
    >
    > > On Apr 19, 5:28 am, Good Man <> wrote:

    >
    > > > ".Net Sports" <> wrote in news:1176950921.045685.277030
    > > > @o5g2000hsb.googlegroups.com:

    >
    > > > > eval(charpos+1)+"]");
    > > > > return false;
    > > > > }//if
    > > > > break;
    > > > > }

    >
    > > > > ...and i'm having trouble figuring how to add the added requirement of
    > > > > "at least one numeric character" constraint.

    >
    > > oops just remove ^ and $

    >
    > > var value = 'hi th4ere';
    > > var RegExp = /[A-Za-z0-9]{6,16}/;

    >
    > > if( RegExp.test(value) )
    > > {
    > > alert( 'yipee' );

    >
    > > }

    >
    > > this /does/ impose the requirement you are after.

    >
    > > > you seem to have more than one problem going on... using 'eval' is highly,
    > > > highly discouraged and quite possibly dangerous.

    >
    > well I see what you are after now, sorry for the confusion - I think
    > you/ were clear - you must use something like
    >
    > var value = 'hith9ere';
    > var RegExp = /^[a-zA-Z0-9]*[0-9]+[a-zA-Z0-9]*$/
    > //or
    > //var RegExp = /^[a-z0-9]*[0-9]+[a-z0-9]*$/i
    > //or
    > //var RegExp = /^\w*\d+\w*$/
    >
    > if( RegExp.test(value) )
    > {
    > //ok
    >
    > }
    >
    > of course this allows 0 or more matches in the ranges a-z A-z 0-9
    > before at least one 0-9 followed by 0 or more matches from the larger
    > range again.
    > you could use the i modifier to reduce the complexity of the reg exp
    > slightly, or in further by using the \w* and \d+ character escapes
    > although \w includes the underscore character which you might not want
    > to allow. The 0-9 is repeated in the ranges because you might want
    > characters from the 0-9 to be repeated non-consecutively. sorry for
    > being needlessly sure of myself before!


    just to clarify, you dont need the + after [0-9] because the * is
    greedy of course, didnt spot that in time.
    so var RegExp = /^\w*\d\w*$/ will do (or equivalent)
     
    shimmyshack, Apr 19, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,380
  2. John Saunders

    Re: alpha numeric sorting for dataview

    John Saunders, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,736
    John Saunders
    Jul 20, 2004
  3. shan

    declaring alpha numeric

    shan, Nov 16, 2005, in forum: C Programming
    Replies:
    11
    Views:
    938
    Simon Biber
    Nov 17, 2005
  4. Replies:
    5
    Views:
    6,400
    Daniele Futtorovic
    Aug 19, 2008
  5. John Smith
    Replies:
    8
    Views:
    188
    David K. Wall
    Sep 26, 2003
Loading...

Share This Page