Best way to alphanumeric check in JavaScript

后端 未结 11 2254
逝去的感伤
逝去的感伤 2020-11-28 05:09

What is the best way to perform an alphanumeric check on an INPUT field in JSP? I have attached my current code



        
11条回答
  •  青春惊慌失措
    2020-11-28 05:48

    Check it with a regex.

    Javascript regexen don't have POSIX character classes, so you have to write character ranges manually:

    if (!input_string.match(/^[0-9a-z]+$/))
      show_error_or_something()
    

    Here ^ means beginning of string and $ means end of string, and [0-9a-z]+ means one or more of character from 0 to 9 OR from a to z.

    More information on Javascript regexen here: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

提交回复
热议问题