JavaScript中的正则表达式代码,用于限制输入字段 [英] Regex code in javascript for restricting input field

查看:125
本文介绍了JavaScript中的正则表达式代码,用于限制输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于限制在某些输入字段中输入的值的问题.我想输入这个:

I have a question about restricting the value entered in some input field. I would like to input this :

9位数字-例如101010101-测试字符串.

9digits-whatever, for example 101010101-Teststring.

我想出了这个正则表达式作为解决方案,但这似乎并不正确:

I have come up with this regex as a solution, but it does not seem to be correct:

^\d{9}\-[a-zA-Z0-9_]*$

对此有什么想法吗?

谢谢.

推荐答案

下面的正则表达式将匹配-跟随的9位数字和任意数量的字符.

The below regex would match 9 digits follwed by - and any number of characters.,

^\d{9}-.*$

如果您希望至少一个字符后接-,请尝试以下正则表达式.

If you want a minimum of one character followed by -, then try the below regex.

^\d{9}-.+$

说明:

  • ^断言我们位于这一行的开头.
  • \d{9}恰好9位数字.
  • \-文字-符号.
  • .*匹配任何字符零次或多次.
  • $行的结尾.
  • ^ Asserts that we are at the beginning of the line.
  • \d{9} Exactly 9 digits.
  • \- Literal - symbol.
  • .* Matches any character zero or more times.
  • $ End of the line.

这篇关于JavaScript中的正则表达式代码,用于限制输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆