Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To check if a string is alphanumeric in javascript

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.

like image 379
user1504606 Avatar asked Apr 30 '13 11:04

user1504606


People also ask

How do you know if a string is alphanumeric?

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)!

Is alphanumeric in JavaScript?

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.

How do you check if a string contains only alphabets and numbers in JavaScript?

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 .

How do I restrict only alphanumeric in JavaScript?

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.


1 Answers

Try this regex:

/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+[0-9a-z]+$/i

Which allows only Alphanumeric.

It doesn't allow:

  • Only Alpha
  • Only Numbers

Refer LIVE DEMO

Updated:

Below regex allows:

/^([0-9]|[a-z])+([0-9a-z]+)$/i
  • AlphaNumeric
  • Only Alpha
  • Only Numbers
like image 109
Siva Charan Avatar answered Sep 28 '22 04:09

Siva Charan