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 ="[^A-Za-z0-9\-_]");

    but doing:

    var charpos ="[^A-Za-z0-9]");

    ....doesnt work.

    .Net Sports, Apr 18, 2007
    1. Advertisements

  2. .Net Sports

    shimmyshack Guest

    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
    1. Advertisements

  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 ="[^A-Za-z]");
    if(objValue.value.length > 0 && charpos >= 0)
    if(!strError || strError.length ==0)
    strError =": Only alphabetic
    characters allowed ";
    alert(strError + "\n [Error character position " +
    return false;

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

    .Net Sports, Apr 19, 2007
  4. .Net Sports

    shimmyshack Guest

    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.
    shimmyshack, Apr 19, 2007
  5. .Net Sports

    Lee Guest

    shimmyshack said:
    How does that require at least one numeric character?

    Lee, Apr 19, 2007
  6. In comp.lang.javascript message <>, Thu, 19 Apr 2007 01:50:27, shimmyshack

    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.
    Dr J R Stockton, Apr 19, 2007
  7. .Net Sports

    shimmyshack Guest

    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]*$/
    //var RegExp = /^[a-z0-9]*[0-9]+[a-z0-9]*$/i
    //var RegExp = /^\w*\d+\w*$/

    if( RegExp.test(value) )

    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
  8. .Net Sports

    shimmyshack Guest

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.