1

I'm new to Angular.js 2 and i want to check a input value for age that if the value was lower than 18 it shows the danger text I tried this but it didn't work can you give me a solution or correct the code?

@Component({
    selector: 'myForm',
    template:`
<div class="form-group">
        <label>age</label>
        <input type="number" class="form-control" [(ngModel)]="age">
        <p class="text-danger" [style.display]="ageCheck(54) ? 'block' : 'none' ">You cannot register you're under 18!</p>
</div>
`
})

export class FormComponent {

ageCheck(age: number){
    if(age>=18) {
        return true;
    }
    return false;
}

3 Answers 3

1

I tend to use ng-show or ng-if in that case instead of playing directly with the style. This would look like (not tested):

<div class="form-group">
    <label>age</label>
    <input type="number" class="form-control" ng-model="age">
    <p class="text-danger" ng-show="age>18">{{age}}</P
    <p class="text-danger" ng-show="age<=18">You cannot register you're under 18!</p>
</div>

That would do exactly what you ask above. Since this looks more like a validation you can create your validator to be more inline with angular philosophy by implementind a directive in your module.

myModule.directive('ageCheck', function() {
    return {
        require: "ngModel",
        link: function(scope,element,attributes,ngModel) {
            ngModel.$validators.ageCheck=function(modelValue) {
                return scope.age>18;
            }
        }
    }
});

<form name="action">
<div class="form-group">
<label>age</label>
<input type="number" name="ageField" class="form-control" ng-model="age" age-check>
<p class="text-danger" ng-show="action.ageField.$error.ageCheck">You cannot register you're under 18!</p>
</div>

If you are new to Angular I strongly suggest to go through their official tutorial which is much better then others I did before. It really brings help me understand the injector/module/minification. You should also start your project using the seed.

Sign up to request clarification or add additional context in comments.

1 Comment

This is to inform you that this question belongs to Angular2. You have come up with Angular1 solution.
1

Use the min

        <label for="age">Age</label>

        <input type="number" min="18" class="form-control" required
          [(ngModel)]="model.age" name="age"  #age="ngModel" >

        <div [hidden]="age.valid || age.pristine" class="alert alert-danger">
          Age is required, should be 18+
        </div>

Note: user angular 2 RC3 for #age="ngModel" to work

Comments

0

No need to use ageCheck function,

this should work I guess,

<p class="text-danger" [style.display]="age<=18 && age!=undefined ? 'block' : 'none' ">You cannot register you're under 18!</p>

https://plnkr.co/edit/aKqBBly2zlT3pijmnwEM?p=preview

OR

<p class="text-danger" [style.display]="ageCheck(age) ? 'block' : 'none' ">You cannot register you're under 18!</p>

ageCheck(age: number){ 

    if(age<=18 && age!=undefined) {
        return true;
    }
    return false;
}

2 Comments

actually as i said I'm new to angular and I want to practice using function , is there any way to use the function?
Check my link and answer. With and without a function.

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.