首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在coldfusion中使用html进行模式验证的正则表达式

在coldfusion中使用html进行模式验证的正则表达式
EN

Stack Overflow用户
提问于 2015-07-08 23:16:06
回答 1查看 706关注 0票数 1

我有一个7位数长度的变量"CustCd“。第一个数字可以是数字或字母,但最后6个数字(位置2-7)必须是数字。我正在尝试使用一个regex where pattern = "0-9 A-Z a-z{1} 0-9{2,7}“与类validate,但它不工作。

下面是我的代码:

代码语言:javascript
复制
<input type="text" name="CustCd" id="CustCd" 
    size="7" maxlength="7" 
    pattern="[0-9 A-Z a-z]{1} [0-9]{2,7}" 
    title="Customer Code" class="validate required" />

这是我第一次使用正则表达式,所以我想我可能忽略了一些东西。非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-07-09 00:36:18

尝试使用^[a-zA-Z0-9]{1}[0-9]{6}$作为您的模式。请记住,此验证是针对JavaScript regEx引擎进行的。

代码语言:javascript
复制
<input type="text" name="CustCd" id="CustCd" size="7" maxlength="7" 
  pattern="^[a-zA-Z0-9]{1}[0-9]{6}$"  title="Customer Code" class="validate required" />

还要确保页面上有正确的文档类型

代码语言:javascript
复制
<!DOCTYPE html>

通过regex101.com进行解释

代码语言:javascript
复制
^ assert position at start of the string
[a-zA-Z0-9]{1} match a single character present in the list below
  Quantifier: {1} Exactly 1 time (meaningless quantifier)
  a-z a single character in the range between a and z (case sensitive)
  A-Z a single character in the range between A and Z (case sensitive)
  0-9 a single character in the range between 0 and 9
[0-9]{6} match a single character present in the list below
  Quantifier: {6} Exactly 6 times
  0-9 a single character in the range between 0 and 9
$ assert position at end of the string

可以尝试的完整示例代码

代码语言:javascript
复制
<!DOCTYPE html>
<form>
  <input type="text" name="CustCd" id="CustCd" size="7" maxlength="7" pattern="[a-zA-Z0-9]{1}[0-9]{6}" title="Customer Code" class="validate required" />
  <input type="submit">
</form>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31296842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档