M
M
mynameyourname2016-05-13 15:06:16
JavaScript
mynameyourname, 2016-05-13 15:06:16

How to properly validate a field in angular?

There is an input field, if the user enters into it a value that is already in the database, then I prescribe:

$scope.requiredName = false;
$scope.uniqueName = true;

If he enters text, and then deletes it, then I write:
$scope.requiredName = true;
$scope.uniqueName = false;

Changing the value of a field looks like this
$scope.$watch('currentUser.username'
. How can I make the message with the error "News is required" appear after the user clicks on the field and then clicks anywhere on the page (without typing or deleting anything in the field)?
<div>
    <input type="text" placeholder="Username" class="form-control" ng-model="currentUser.username" name="name" />
    <span class="error text-small block" ng-if="Form.name.$dirty && requiredName">News is required</span>
    <span class="error text-small block" ng-if="Form.name.$dirty && uniqueName">News is not unique</span>
</div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2016-05-13
@mynameyourname

ng-blur="yourFunction()"

N
Nicholas, 2016-05-13
@healqq

Changing the value of a field is worth watching through ng-change ( dock on ng-change ).
In order for the model to be updated only on blur, you can set custom ng-model-options ( dock by ng-model-options ).
For validation, you should use the standard angular features for forms, here you can read about them:
dock about forms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question