1

I'm trying to write a Regex What I need is:

  • To start only with: A-z (Alphabetic)
  • Min. Length: 5
  • Max. Length: 10
  • The rest can be A-z0-9(Alphanumeric) but contain at least one number

What I have: ^[A-z][A-z0-9]{5,10}$

SergkeiM
  • 3,934
  • 6
  • 36
  • 69
  • And what is the issue you are facing with current regex? – Rajesh Dec 05 '16 at 09:06
  • You need to use positive lookbehind for the at least one number requirement. – wvdz Dec 05 '16 at 09:07
  • So your issue is about the number requirement? – Fabrizio Stellato Dec 05 '16 at 09:07
  • Wait, does it mean you allow any char in the string after the first letter? Just there must be one digit later? Or should the string be alphanumeric only? Try `/^(?=.{5,10}$)[a-zA-Z]\D*\d\D*$/` if you want to allow any chars (but if you do not expect line break chars in the input). – Wiktor Stribiżew Dec 05 '16 at 09:09
  • @FabrizioStellato My issue is how to determinate characters after first, to validate them for A-z0-9. Or spilt the validation for 2 steps 1 - just the first character 2- the rest of the string and 3 - the whole string just for length – SergkeiM Dec 05 '16 at 09:10
  • Your question is still not clear: what can be there inside the string, except the first letter and a digit somewhere inside? – Wiktor Stribiżew Dec 05 '16 at 09:14
  • @WiktorStribiżew Updated my question, the rest are alphanumeric but contain at least one number – SergkeiM Dec 05 '16 at 09:17

2 Answers2

4

You can use

/^(?=.{5,10}$)[a-z][a-z]*\d[a-z\d]*$/i

See the regex demo

Details:

  • ^ - start of string
  • (?=.{5,10}$) - the string should contain 5 to 10 any chars other than line break chars (this will be restricted by the consuming pattern later) up to the end of string
  • [a-z] - the first char must be an ASCII letter (i modifier makes the pattern case insensitive)
  • [a-z]* - 0+ ASCII letters
  • \d - 1 digit
  • [a-z\d]* - 0+ ASCII letters of digits
  • $ - end of string.

var ss = [ "ABCABCABC1","ABCA1BCAB","A1BCABCA","A1BCAB","A1BCA","A1BC","1BCABCABC1","ABCABC","ABCABCABCD"]; // Test strings
 
var rx = /^(?=.{5,10}$)[a-z][a-z]*\d[a-z\d]*$/i;                  // Build the regex dynamically
document.body.innerHTML += "Pattern: <b>" + rx.source
          + "</b><br/>";                               // Display resulting pattern
for (var s = 0; s < ss.length; s++) {                  // Demo
  document.body.innerHTML += "Testing \"<i>" + ss[s] + "</i>\"... ";
  document.body.innerHTML += "Matched: <b>" + rx.test(ss[s]) + "</b><br/>";
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

var pattern = /^[a-z]{1}\w{4,9}$/i;

/* PATTERN
^ : Start of line
[a-z]{1} : One symbol between a and z
\w{4,9} : 4 to 9 symbols of any alphanumeric type
$ : End of line
/i : Case-insensitive
*/

var tests = [
  "1abcdefghijklmn", //false
  "abcdefghijklmndfvdfvfdv", //false
  "1abcde", //false
  "abcd1", //true
];

for (var i = 0; i < tests.length; i++) {
  console.log(
    tests[i],
    pattern.test(tests[i])
  )
}
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28