【问题标题】:How to prevent input type number from accepting numbers in e format如何防止输入类型数字接受e格式的数字
【发布时间】:2018-05-29 20:44:19
【问题描述】:

我在我的 html 页面中使用输入类型数字,但我不想要 e 格式的数字。我正在尝试使用如下的正则表达式:

$scope.regexInteger = '^[0-9^e]*$';

并尝试使用 ng-messages 使其无效

<input type="number" maxlength="20" class="w3-input w3-border w3-round-large" ng-pattern="regexInteger" name = 'intVal' />
<div ng-messages='intVal' >
<div ng-message="pattern">Invalid Integer</div>
</div>

但我无法使用 ng-messages 使其无效。我想我没有通过正确的正则表达式。

请纠正我哪里出错了。

【问题讨论】:

  • 试试这个正则表达式:/[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/ (source)
  • 创建一个只接受数字的自定义目录。
  • 这个正则表达式不会阻止用户输入'e'。
  • 您通过在组内添加^e 来尝试实现什么?我的猜测是你试图明确地说“不是字符'e'”,这是有道理的,但你已经通过[0-9] 隐含地明白了这一点,它只是说只接受“0”和“之间的字符” 9",即只有数字字符。

标签: angularjs regex html


【解决方案1】:

试试这个.. 这将是一个简单的解决方案,因为您将使用正则表达式,它只允许数字 0-9 而不允许任何其他字符

$scope.regexInteger ='^[0-9]*$'

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2021-12-09
    • 1970-01-01
    • 2013-11-26
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多