RegEx for Javascript to allow only alphanumeric

前端 未结 18 1523
轻奢々
轻奢々 2020-11-22 15:13

I need to find a reg ex that only allows alphanumeric. So far, everyone I try only works if the string is alphanumeric, meaning contains both a letter and a number. I just w

18条回答
  •  盖世英雄少女心
    2020-11-22 15:37

    This will work

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

    It accept only alphanumeriuc characters alone:
    test cases pased :

    dGgs1s23 - valid
    12fUgdf  - valid,
    121232   - invalid, 
    abchfe   - invalid,
     abd()*  - invalid, 
    42232^5$ - invalid
    

    or

    You can also 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

    Refered:Regular expression for alphanumeric in Angularjs

提交回复
热议问题