当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript regex number range format check

问题描述:

This question already has an answer here:

  • Regex - Match whole string

    3 answers

网友答案:

Your regex only check for substring/contains.

Use anchors ^: starts with and $: ends with.

/^\d+-\d+$/

Explanation:

  1. /: Delimiters of regex
  2. ^: Starts with
  3. \d+: Matches one or more numbers
  4. -: Matches - literal
  5. $: Ends with

jsfiddle Demo

<input onblur="alert(/^\d+-\d+$/.test(this.value))">
分享给朋友:
您可能感兴趣的文章:
随机阅读: