简体   繁体   中英

Regular expression for alphanumeric in Angularjs

I want a regex for alphanumeric characters in angularJS, I've tried some regex like "(\\d[az])" 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.

这需要至少一个(az,AZ和0-9)之一:

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

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

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 az, AZ, 0-9, Space, Underscore and dash.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM