Search code examples
angularjspromptalphanumeric

Allowing only alphanumeric characters through prompt box in angularjs


I have an angularjs function which provides a promptbox to the user to enter a name. I want the user to not to enter any non-alphanumeric values. For a promptbox how can I restrict the input? Please find the below function which gives the promptbox to the user.

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

Solution

  • I used regex expression and it works successfully.Please find the following code.

    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");
    }
    }