首页 > 解决方案 > angularjs中的提示框只允许字母数字字符

问题描述

我有一个 angularjs 函数,它为用户提供输入名称的提示框。我希望用户不要输入任何非字母数字值。对于提示框,如何限制输入?请找到以下向用户提供提示框的功能。

$scope.userInput = function(inputName)
{
  var name = prompt("Please Enter your name", inputName);
};

标签: angularjspromptalphanumeric

解决方案


我使用了正则表达式,它成功了。请找到以下代码。

js

$scope.userInput = function(inputName)
{
  var name = prompt("Please Enter your name", inputName);
var regPattern = /^[A-Za-z0-9]{2,}$/;
var regResult = regPattern.test(inputName);
if(regResult === true)
{
alert("correct name is given");
}
}

推荐阅读