Regex matches but shouldn't

前端 未结 2 702
自闭症患者
自闭症患者 2021-01-21 12:47

This is my code that checks if there are only alphanumeric characters filled in, but when i enter something like adasd@#$ it still matches and i have no idea why. Any idea how t

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-21 13:44

    Your regex [0-9a-zA-Z] checks for any alphanumeric character in the input string. Since it finds a, d, a, s, d in your input string, it returns true.
    What you need to do is place start and end enchors in your regex. New regex would look like this:

    ^[0-9a-zA-Z]+$
    

提交回复
热议问题