I want to check if a string is STRICTLY ALPHANUMERIC in javascript. i have tried this:
var Exp = /^[0-9a-z]+$/;
if(!pwd.match(Exp))
alert("ERROR")
But the problem with this is it passes input sting if it contains all alphabet or all numeric, i want the function to pass if it contains both Characters and Numbers.
The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9). Example of characters that are not alphanumeric: (space)!
What Is Alphanumeric Validation In Javascript? First let's see what is alphanumeric. Alphanumeric is any alphabet from (A to Z or a to z) or any number from (0 to 9) so alphanumeric validation makes sure that all the characters entered in an input are alphanumeric.
The RegExp test() Method To check if a string contains only letters and numbers in JavaScript, call the test() method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .
You will use the given regular expression to validate user input to allow only alphanumeric characters. Alphanumeric characters are all the alphabets and numbers, i.e., letters A–Z, a–z, and digits 0–9.
Try this regex:
/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i
Which allows only Alphanumeric.
It doesn't allow:
Refer LIVE DEMO
Below regex allows:
/^([0-9]|[a-z])+([0-9a-z]+)$/i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With