用于IP地址验证的Angular JS中的ng-pattern

时间:2015-08-28 11:19:07

标签: javascript html regex angularjs

我正在尝试使用ng-pattern

在文本框中验证IP地址

CODE:

<input name="machinestodiscover" type="text" ng-model="machinestodiscover" ng-minlength="7" ng-maxlength="15" ng-pattern="/\b([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})\b/" required>
 <span ng-show="conditionForm.machinestodiscover.$error.required" class="help-block" style="display:inline;">*Machines To discover Required</span>
 <span ng-show="conditionForm.machinestodiscover.$error.pattern" class="help-block" style="display:inline;">*IP Pattern Wrong.</span>

我面临的问题是它甚至接受了1.1.1.1.1.1.1的价值。

我检查了http://regexr.com/

中的表达式

截图:

enter image description here

enter image description here

enter image description here

我的正则表达式/ ng-pattern

有什么问题

1 个答案:

答案 0 :(得分:2)

问题出在.这是字边界。即^([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})[.]([0-9]{1,3})$ 也与单词边界相匹配。

改为使用锚点

^
  • $将正则表达式锚定在字符串的开头。
  • SELECT Positionname, Count(NoOfApplication) AS NoOfApplications, Max(COUNT1) AS Qualified, Max(COUNT2) AS Diqualified FROM APPLICATION A CROSS APPLY (SELECT Count(NoOfApplication) AS COUNT1 FROM APPLICATION A WHERE EligibleStatus1 = 1 AND EligibleStatus2 = 1)B CROSS APPLY (SELECT Count(NoOfApplication) AS COUNT2 FROM APPLICATION A WHERE EligibleStatus1 = 2 OR EligibleStatus2 = 2)C GROUP BY PositionName 将正则表达式锚定在字符串的末尾

Regex Demo