0

I am trying to check if an input value is alphanumeric. According to regex101.com my regex should work. But all test results in "incorrect".

What am I doing wrong?

var alphaNumeric = /^[a-z0-9]+$/gi;
var input = "123"; //page.input.getValue().toUpperCase();

console.log(input);

if (input.length == 3) {

  if (alphaNumeric.test(input)) {
    console.log("correct");
  } else {
    console.log("incorrect");
  }
} else {

}
Hary
  • 5,690
  • 7
  • 42
  • 79
Joep
  • 15
  • 3
  • Possible duplicate of [RegEx for Javascript to allow only alphanumeric](https://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric) – Rui Jarimba Nov 01 '18 at 12:43
  • 1
    `But all test results in "incorrect".` can give sample inputs that don't work? – VLAZ Nov 01 '18 at 12:45
  • 1
    It's not a duplicate. Please read my question. @RuiJarimba – Joep Nov 01 '18 at 12:45
  • 2
    `page` is not defined, and what is `getValue()`? – Andy Nov 01 '18 at 12:45
  • 1
    It works! What output you got? – Hary Nov 01 '18 at 12:46
  • 123, AAA, !!! - These all results in incorrect. – Joep Nov 01 '18 at 12:46
  • @Andy it is an function of IBM forms to get an input value. The function works fine. – Joep Nov 01 '18 at 12:47
  • Your code is incomplete, as others have stated, so we are unable to test it with a view to reproducing the incorrect behaviour. I would suspect that you are not updating the value of `input` when the user submits the string to test? – Robin Zigmond Nov 01 '18 at 12:48
  • Just tried with "123" and "AAA" and it works. – simondvt Nov 01 '18 at 12:49
  • @RobinZigmond that's what the getValue() function does. I get the updated value but the if statement always results in incorrect. – Joep Nov 01 '18 at 12:50
  • I have updated your question, please try with Run code Snippet – Hary Nov 01 '18 at 12:50
  • well it works, returning "correct". @Joep - I know `getValue()` gets the current input, but unless you are calling that and putting its return value into the `input` variable, at the point you test, it'll just be testing against whatever the initial input value was. (Which I guess is the empty string.) – Robin Zigmond Nov 01 '18 at 12:51
  • All thanks for your help. The issue had to do with the /g of the regex. @Noninka was correct. My new regex is: /^[a-z0-9]+$/i; – Joep Nov 01 '18 at 13:10

2 Answers2

0

See the answer of @bobince: https://stackoverflow.com/a/2630538/1241218

You're using a g (global) RegExp. In JavaScript, global regexen have state: you call them (with exec, test etc.) the first time, you get the first match in a given string. Call them again and you get the next match, and so on until you get no match and it resets to the start of the next string. You can also write regex.lastIndex= 0 to reset this state.

(This is of course an absolutely terrible piece of design, guaranteed to confuse and cause weird errors. Welcome to JavaScript!)

You can omit the g from your RegExp, since you're only testing for one match.

So in your case you will have different results on every try:

    var alphaNumeric = /^[a-z0-9]+$/gi;
    var input = "123";
    console.log(input);
    if (input.length == 3) {
        for (var i=0; i<5; i++){
            if (alphaNumeric.test(input)) {
                console.log("correct");
            } else {
                console.log("incorrect");
            }
        }

    } else {
        
    }
Nonika
  • 2,490
  • 13
  • 15
-1

Get rid of the global flag (you're matching start and end anyway)

var alphaNumeric = /^[a-z0-9]+$/i;
console.log(alphaNumeric.test("aaa")); // true
console.log(alphaNumeric.test("AAA")); // true
console.log(alphaNumeric.test("123")); // true
console.log(alphaNumeric.test("a1a1a1")); // true
console.log(alphaNumeric.test("ABC-123")); // false
fhollste
  • 785
  • 6
  • 16