Allowing alphanumeric and dot in scope

10,289

Solution 1

    if (/[^a-zA-Z0-9\.]/.test($scope.test)) {
        alert("Please use only Alphanumeric characters or dots")
    }

Solution 2

The better way is to define a ng-pattern on the html input element. This would not allow incorrect value to be set on model. I have not tested the regular expression pattern.

<input type="text" ng-model="test" ng-pattern="/[a-zA-Z0-9\.]*/"/>
Share:
10,289
babtech
Author by

babtech

Updated on June 08, 2022

Comments

  • babtech
    babtech almost 2 years

    How can I allow only Alphanumeric characters and dots to a scope

    <input type="text" ng-model="test" />
    

    script

    $scope.myFn = function(){
    if($scope.test != ''){
    alert("Please use only Alphanumeric characters or dots")
    }
    
    }