0

I have following code:

Javascript code

<script>
    angular.module('myApp', [])
        .config(function ($interpolateProvider) {$interpolateProvider.startSymbol('{[{').endSymbol('}]}')})
        .controller('myCtrl', function ($scope, $http) {
            $scope.word = '';
            $scope.resp = {};
            $scope.warning = '';
            $scope.get = function () {
                if($scope.word.$invalid){
                    $scope.warning = "Please write single word";
                }else {
                    $http.get("localhost/" + $scope.word)
                            .then(function (response) {
                                $scope.resp = response.data;
                            });
                }
            }
        });
</script>

Html code

<div ng-app="myApp" ng-controller="myCtrl" >
    <div ng-show="word.$invalid" class="alert alert-warning">
        <strong>Warning!</strong> {[{warning}]}.
    </div>
    <input  id="filter_input" type="text" ng-model="word" ng-pattern="^[a-zA-Z]*$"/>
    <br/>
    <input id="btn" type="button" ng-click="get()" value="Check Level">
    <br/>
    <label style="text-align: center">{[{resp.level}]}</label>
</div>

I would like to show warning message, when user fill in input with more than one word. Could you please assist.

1 Answer 1

1

Split on a space character - check the result:

var words = $scope.word.split(" ");
if (words && words.length && words.length === 1) {
    //good to go
} else {
    //error, enter only 1 word
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.