-3

the regular expression: /([a-z][0-9]*){6,12}/i

so i am expecting this to return true if a string contains more than 6 and less than 12 characters even if there aren't 6 alphabetical characters, but it doesn't, i want "123456789a" to return true and "abcdefghi1", but the first one doesn't.

var myRegEx = /([a-z][0-9]*){6,12}/i;


function checkIt() {
var myString = document.getElementsByTagName("input")[0].value;

if(myRegEx.test(myString) == true) {
document.getElementsByTagName("p")[0].className = "trueOrFalse true";
document.getElementsByTagName("p")[0].innerHTML = "True";
}
else {
document.getElementsByTagName("p")[0].className = "trueOrFalse false";
document.getElementsByTagName("p")[0].innerHTML = "False";
}

}
doubleOrt
  • 2,407
  • 1
  • 14
  • 34

2 Answers2

0

https://jsfiddle.net/y71mudms/2/

the correct regex is

var myRegEx = /^[0-9]{6,11}[a-z]{1}$/i;

we search exact match of digits + 1 char from 6 to 12

/^[0-9]{6,11}[a-z]{1}$/i;
 ^  ^    ^           ^
 |  |    |           |_ end of string
 |  |    |
 |  |    |_ from 6 min to 11 max repetitions
 |  |
 |  |_ digit 0-9 include all digits 
 |
 |_ begin of string

ref. 6 digits regular expression

Community
  • 1
  • 1
carlo denaro
  • 425
  • 6
  • 14
  • here let me explain: i want the regex to return true if there are more than 6 and less than 12 alphabetic characters or a mix of alphabetic and numeric characters, but not numeric characters alone. – doubleOrt Jul 26 '16 at 08:46
  • i can acheve this with " /([a-z][0-9]*){6,12}/i" but the problem is, the numbers don't count, so it doesn't return true until there are at least 6 a-z characters, i want it to return true even if there's one a-z character and the rest are numbers. – doubleOrt Jul 26 '16 at 08:51
-1

Just wrap a check for the string-length around your regex-conditions:

if string.length > 6 && string.length < 12) {
  // if regex...
}
fmue
  • 89
  • 6