2

I want a regex for alphanumeric characters in angularJS, I've tried some regex like "(\d[a-z])" but they allow me to enter only number as well as only alphabets. But I want a regex which won't allow me to enter them.

Example: 121232, abchfe, abd()*, 42232^5$ are some example of invalid input. 12fUgdf, dGgs1s23 are some valid inputs.

Ruchi yadav
  • 223
  • 3
  • 15
  • Take a look http://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric – Pravesh Khatri Jul 01 '16 at 06:13
  • Similar questions asked here http://stackoverflow.com/questions/18692187/regex-to-check-if-string-contains-alphanumeric-characters-and-spaces-only-java – S. Divya Jul 01 '16 at 06:17
  • You want a regex that allows only digits and letters, and requires at least one of each? – nnnnnn Jul 01 '16 at 06:23

4 Answers4

2

This one requires atleast one of each (a-z, A-Z and 0-9):

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]+)$
Arg0n
  • 8,283
  • 2
  • 21
  • 38
1

You can try this one. this expression satisfied at least one number and one character and no other special characters

^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$

in angular can test like:

$scope.str = '12fUgdf';
var pattern = new RegExp('^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$');
$scope.testResult = pattern.test($scope.str);

PLUNKER DEMO

Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
0

If you wanted to return a replaced result, then this would work:

var a = 'Test123*** TEST';
var b = a.replace(/[^a-z0-9]/gi,'');
console.log(b);

This would return:

Test123TEST

OR

/^([a-zA-Z0-9 _-]+)$/

the above regex allows spaces in side a string and restrict special characters.It Only allows a-z, A-Z, 0-9, Space, Underscore and dash.

Slan
  • 545
  • 2
  • 6
  • 18
0

try this one : ^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$

dGgs1s23-valid
12fUgdf-valid,
121232-invalid, 
abchfe-in valid,
 abd()*- invalid, 
42232^5$- invalid
Skull
  • 1,204
  • 16
  • 28