Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)

后端 未结 8 1984
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 04:05

I\'m currently using this regex ^[A-Z0-9 _]*$ to accept letters, numbers, spaces and underscores. I need to modify it to require at least one number or letter s

相关标签:
8条回答
  • 2020-12-08 04:16

    You simply need to specify your current RE, followed by a letter/number followed by your current RE again:

    ^[A-Z0-9 _]*[A-Z0-9][A-Z0-9 _]*$
    

    Since you've now stated they're Javascript REs, there's a useful site here where you can test the RE against input data.

    If you want lowercase letters as well:

    ^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$
    
    0 讨论(0)
  • 2020-12-08 04:17

    To go ahead and get a point out there, instead of repeatedly using these:

    [A-Za-z0-9 _]
    [A-Za-z0-9]
    

    I have two (hopefully better) replacements for those two:

    [\w ]
    [^\W_]
    

    The first one matches any word character (alphanumeric and _, as well as Unicode) and the space. The second matches anything that isn't a non-word character or an underscore (alphanumeric only, as well as Unicode).

    If you don't want Unicode matching, then stick with the other answers. But these just look easier on the eyes (in my opinion). Taking the "preferred" answer as of this writing and using the shorter regexes gives us:

    ^[\w ]*[^\W_][\w ]*$
    

    Perhaps more readable, perhaps less. Certainly shorter. Your choice.

    EDIT:

    Just as a note, I am assuming Perl-style regexes here. Your regex engine may or may not support things like \w and \W.

    EDIT 2:

    Tested mine with the JS regex tester that someone linked to and some basic examples worked fine. Didn't do anything extensive, just wanted to make sure that \w and \W worked fine in JS.

    EDIT 3:

    Having tried to test some Unicode with the JS regex tester site, I've discovered the problem: that page uses ISO instead of Unicode. No wonder my Japanese input didn't match. Oh well, that shouldn't be difficult to fix:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    

    Or so. I don't know what should be done as far as JavaScript, but I'm sure it's not hard.

    0 讨论(0)
  • 2020-12-08 04:17

    Simply u can add this to jquery.validationEngine-en.js file

        "onlyLetterNumberSp": {
                    "regex": ^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$,
                    "alertText": "* No special characters allowed"
              },
    

    and call it in text field as

    <input type="text" class="form-control validate[required,custom[onlyLetterNumberSp]]"  id="title" name="title" placeholder="Title"/>
    
    0 讨论(0)
  • 2020-12-08 04:20
    ^[ _]*[A-Z0-9][A-Z0-9 _]*$
    

    You can optionally have some spaces or underscores up front, then you need one letter or number, and then an arbitrary number of numbers, letters, spaces or underscores after that.

    Something that contains only spaces and underscores will fail the [A-Z0-9] portion.

    0 讨论(0)
  • 2020-12-08 04:22

    for me @"^[\w ]+$" is working, allow number, alphabet and space, but need to type at least one letter or number.

    0 讨论(0)
  • 2020-12-08 04:23
    $("#ValuationName").bind("keypress", function (event) {
        if (event.charCode!=0) {
            var regex = new RegExp("^[a-zA-Z ]+$");
            var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
            if (!regex.test(key)) {
                event.preventDefault();
                return false;
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题