RegEx for matching “A-Z, a-z, 0-9, _” and “.”

后端 未结 5 2109
耶瑟儿~
耶瑟儿~ 2021-02-01 14:13

I need a regex which will allow only A-Z, a-z, 0-9, the _ character, and dot (.) in the input.

I tried:

[A-Za-z0-9_.] 

But, it did not

5条回答
  •  甜味超标
    2021-02-01 14:57

    Working from what you've given I'll assume you want to check that someone has NOT entered any letters other than the ones you've listed. For that to work you want to search for any characters other than those listed:

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

    And use that in a match in your code, something like:

    if ( /[^A-Za-z0-9_.]/.match( your_input_string ) ) {
       alert( "you have entered invalid data" );
    }
    

    Hows that?

提交回复
热议问题