Reputation: 563
I am using the following regex - [a-zA-Z0-9]{9,18}
which means I can use alphabets and numbers with minimum length as 9 and maximum length as 18. It should not take special characters.
It takes values like ADV0098890
etc. but it is also taking ADV0098890[]
, which is wrong.
How can I prevent that?
Upvotes: 1
Views: 39
Reputation: 7213
Your regex is matching only the first part of the string. Try wrapping the pattern in ^$
:
>> !!('ADV0098890' =~ /[a-zA-Z0-9]{9,18}/)
=> true
>> !!('ADV0098890[]' =~ /[a-zA-Z0-9]{9,18}/)
=> true
>> !!('ADV0098890' =~ /^[a-zA-Z0-9]{9,18}$/)
=> true
>> !!('ADV0098890[]' =~ /^[a-zA-Z0-9]{9,18}$/)
=> false
Upvotes: 1