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

时间:2015-07-08 15:16:06

标签: regex validation coldfusion

我有一个长度为7位的变量“CustCd”。第一个数字可以是数字或字母,但最后6位数字(位置2-7)必须是数字。我正在尝试使用正则表达式,其中pattern =“[0-9 A-Z a-z] {1} [0-9] {2,7}”需要进行类验证,但它无效。

这是我的代码:

Dim exclude = { "00000000.001", "00000000.002" }

Dim dirs As DirectoryInfo() = dir2.EnumerateDirectories().
    Where(Function(dir) Not exclude.Any(Function(d) dir.Name.Contains(d))).
    ToArray()

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

1 个答案:

答案 0 :(得分:1)

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

<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" />

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

<!DOCTYPE html>

通过regex101.com

解释
^ 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

尝试的完整示例代码

<!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>