简体   繁体   中英

Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number

I'm trying to generate a regular expression which can satisfy following conditions:

  1. It should be alphanumeric only.
  2. It should contains at least 1 alphabet and at least 1 number.
  3. It should allow only small letters.
  4. It's length should be between 3 and 16 inclusive.
  5. The first character should be an alphabet.

Ex:

  1. abc - invalid (should have at least 1 number also)
  2. ab9 - valid
  3. 9ab - invalid (should start with an alphabet)
  4. Ab9 - invalid (No Capital letters allowed)
  5. aa - invalid (length should be at least 3)
  6. abcdefghijklmnop99 - invalid (length should be at least 16)

I tried following solution but it's not working as expected

/^(?=.*[a-z])[a-z0-9]{3,16}$/

I want to use this regex in ng-pattern to check text input from user.

I assume you've misstated your problem, and that it's not

Regular Expression for alphnumeric with first alphabet and atlest one alphabet and one number

but actually

Way to test a string for alphanumeric with first alphabet and at least one alphabet and one number

and that you only mention regular expressions because you just imagined that that must be the best, or only, way to solve it. It's not .

You can write one big regular expression which does the job--although another SO member who regularly answers regexp questions and has a high rep could not do it correctly on his first answer (since deleted)--but then remember, you'll have to maintain it and modify it when the rules change.

There may be cases where something simply must be a single regular expression. An example is where you are using a library, and the library only takes a regular expression as the way to test or match something. However, that is not often the case, and I doubt it is here either.

A good basic rule of programming is just to write down the rules. Another good basic rule is that after you write them down, you should be able to read them back. So we start off by writing your problem as:

function pass(str) {
    return alphanumeric(str) &&
           one_alphabet(str) &&
           one_number  (str) &&
           right_length(str) &&
           first_alpha (str);
}

One can tell at a glance what this does, and that it is correct. Your boss could probably even figure it out. Naming the individual conditions like this also constitutes a kind of documentation for ourselves and those who come later.

Now just write each little function, something even those with rudimentary regexp skills can do:

function alphanumeric(str) { return !/[^a-z0-9]/.test(str); }
function one_alphabet(str) { return /a-z/       .test(str); }
function one_number  (str) { return /\d/        .test(str); }
function first_alpha (str) { return /^[a-z]/    .test(str); }
function right_length(str) { var l = str.length; return l >=3 && l <= 16; }

Actually, if first_alpha passes, then so will one_alphabet , so we can omit that test.

If you wanted to get fancy, you could do something like:

var rules = [ alphanumeric, one_number, right_length, first_alpha];

function pass(str) { 
    return rules.all(function(rule) { return rule(str); }); 
}

Now adding a new rule is as simple as adding one element to the rules array.

Asynchronous checks

One of the many advantages of writing things this way is that it could easily handle asynchronous checks--let's say you wanted to check that the password had not been used before, which requires a round trip to the server. So we write

var rules = [ alphanumeric, one_number, right_length, first_alpha, not_used];
function not_used(str) { return ajax(...); }

Then we can write pass as

function pass(str) {
    return Promise.all(rules.map(function(rule) { return rule(str); }) .
        then (function(results) { console.log("Valid: ", results.all(Boolean)); }) . 
        catch(function()        { console.log("Execution of rule failed"); });
}

Last time I checked, regexp's could not handle asynchronous checks.

I'd use:

/^[a-z](?=.*\d)[a-z\d]{2,15}$/

or, if you want to be unicode compatible:

/^\p{Ll}(?=.*\p{N})[\p{Ll}\p{N}]{2,15}$/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM