javascript regex for special characters

前端 未结 12 786
囚心锁ツ
囚心锁ツ 2020-11-29 22:28

I\'m trying to create a validation for a password field which allows only the a-zA-Z0-9 characters and .!@#$%^&*()_+-=

I can\'t seem to

相关标签:
12条回答
  • 2020-11-29 22:55
    // Regex for special symbols
    
    var regex_symbols= /[-!$%^&*()_+|~=`{}\[\]:\/;<>?,.@#]/;
    
    0 讨论(0)
  • 2020-11-29 23:02

    How about this:-

    var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,}$/;
    

    It will allow a minimum of 6 characters including numbers, alphabets, and special characters

    0 讨论(0)
  • 2020-11-29 23:03
    var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g
    

    Should work

    Also may want to have a minimum length i.e. 6 characters

    var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]{6,}$/g
    
    0 讨论(0)
  • 2020-11-29 23:04

    You can be specific by testing for not valid characters. This will return true for anything not alphanumeric and space:

    var specials = /[^A-Za-z 0-9]/g;
    return specials.test(input.val());
    
    0 讨论(0)
  • 2020-11-29 23:06

    There are some issue with above written Regex.

    This works perfectly.

    ^[a-zA-Z\d\-_.,\s]+$
    

    Only allowed special characters are included here and can be extended after comma.

    0 讨论(0)
  • 2020-11-29 23:06

    This regex works well for me to validate password:

    /[ !"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/
    

    This list of special characters (including white space and punctuation) was taken from here: https://www.owasp.org/index.php/Password_special_characters. It was changed a bit, cause backslash ('\') and closing bracket (']') had to be escaped for proper work of the regex. That's why two additional backslash characters were added.

    0 讨论(0)
提交回复
热议问题