Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AngularJs仅允许文本框中的字符_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 使用AngularJs仅允许文本框中的字符

Javascript 使用AngularJs仅允许文本框中的字符,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我只想在我的项目中验证textbox,这样用户就不能输入字符以外的任何值。Hackish?way,$在控制器中观看ng型号: <input type="text" ng-model="myText"> $scope.$watch('myText', function() { // put logic to strip out all non-character characters here if ($scope.myText ... regex to look fo

我只想在我的项目中验证textbox,这样用户就不能输入字符以外的任何值。

Hackish?way,$在控制器中观看ng型号:

<input type="text" ng-model="myText">
$scope.$watch('myText', function() {
   // put logic to strip out all non-character characters here
   if ($scope.myText  ... regex to look for ... ) {
      // strip out the non-characters
   }
})
最好的方法是在指令中使用$parser。 我不打算重复@pkozlowski.opensource提供的已经很好的答案,下面是链接:

不要试图使用ng change,它会导致问题。看

更新:
也可用于定义限制字段中允许的内容的正则表达式。另请参见。

您似乎不理解我的问题。我希望该用户不能在文本框中输入除字符以外的任何值。所以我认为这里我们必须使用onkeypress函数。你说什么?我相信我提供的两个解决方案应该有效。我刚刚用ng模式的信息更新了我的答案。如果您无法使用这三种解决方案之一使其工作,请发布您尝试的一个或多个。我已经使用ng模式用于此目的&它很好地满足了我的验证目的,但实际上现在我希望用户甚至不能在文本框中键入除字符以外的任何内容。你能帮我解决这个问题吗?看看在哪里写了一个指令来防止输入非数字字符。只需将指令中的正则表达式更改为只允许字符。