1

I'm trying to understand a way with angular where I can only let the user enter a number that isn't more than 24 or in my other input 60 to represent hours and minutes.

            <input type="number"
                   placeholder="HH"
                   ng-minlength="2"
                   ng-maxlength="2"
                   required
                   ng-model="session.timeHH">

            <input type="number"
                   placeholder="mm"
                   ng-minlength="2"
                   required
                   ng-maxlength="2"
                   ng-model="session.timeMM">

This is my HTML at the moment.

The user can enter in the hours input a number like 99 which is obviously going to be wrong. I'm looking for a way to validate or prevent the user before submit to only be able to enter numbers up to 24 for hours and 59 for minutes.

Or even on the click of the submit button would be sufficient.

1
  • Well, if it's enough to just validate, you could use min and max attributes. Commented May 18, 2016 at 16:27

5 Answers 5

8

Not angular, but why not use the HTML attributes? Angular is hip to these and you can check the $valid property of the form to make sure the constraints are satisfied.

<input type="number" min="0" max="24" step="1">
Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain to me what the step attribute does please?
@MaxLynn It makes sure that everytime a user clicks on + or - it will increase or decrease with 1
@MaxLynn Setting step=1 indicates that you want an integer. You'd use step=.01 for currency. It's the interval between valid values. And also what Mikey said.
Thanks I didn't know this
2

How about just HTML5 since you're already using type="number":

<input type="number" name="hours" min="1" max="24">

Be sure to also validate input on the server side.

Comments

1

If you want to restrict numbers use a dropdown, otherwise you'll have to use native min and max attributes with validation.

3 Comments

That's a poor user experience IMO.
@Casey its better than letting users input false data and having to re-enter it when the form is submitted and validation fails
He's using angular; there's no reason the user can't have that validation as he enters or blurs the field. You don't have to wait for a submit.
1

Why not make use of the type properties to the inputs as time, this will open a time selector on modern browsers and mobiles

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

         <input type="time"
               placeholder="HH:MM"
               required
               ng-model="session.timeHH">

Comments

0

You can use some angular mask lib that provides masking features.

The ones you could use are, for example, angular-input-masks, ui-mask or ngMask.

3 Comments

Not sure I can justify a lib for this
Yes, these exist, but that's inappropriate for this problem.
Indeed. That's just an option.

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.