Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly validate number ranges in HTML Input? (Angular 2)

In my Angular 2 application I have a component with a input field which is supposed to accept a range of numbers.

More specifically, 2 cases:

  • range 0[0]-23 (hours)
  • range O[0]-59 (minutes)

I am using

<form>
    <input type="text" pattern="[0-9]"> <!-- 0-9 -->
    <input type="text" pattern="\d|1\d|2[0-3]"> <!--  0-23 -->
    <input type="text" pattern="\d\d"> <!--  [0-99] -->
</form>

The issue is that I can basically input anything (as if validation was ignored), including text. I don't think it's an issue related to Angular 2 since standard validation works, e.g.

 <input type="number"> 

allows to input only numbers (but any number which is not what I want)

I also tried with min=0 and max=23 (or 59) attributes with type number but that doesn't work either.

like image 967
dragonmnl Avatar asked May 13 '16 14:05

dragonmnl


People also ask

How do you validate a number range in HTML?

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below. Tip: Always add the <label> tag for best accessibility practices!

How do you input validate in HTML?

The simplest HTML5 validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

How do you validate a number field?

Approach: We have used isNaN() function for validation of the textfield for numeric value only. Text-field data is passed in the function and if passed data is number then isNan() returns true and if data is not number or combination of both number and alphabets then it returns false.

How do you validate input?

Validation should aim to be as accommodating as possible of different forms of input for particular data types. For example, telephone numbers are written with different separators and digit groupings. Your form will be easier to use if it can interpret multiple notations. Also, it is helpful to be liberal with input.


2 Answers

<form>
    <input type="number" min="0" max="9"> <!-- 0-9 -->
    <input type="number" min="0" max="23"> <!--  0-23 -->
    <input type="number" min="0" max="99"> <!--  [0-99] -->
</form>
like image 159
Günter Zöchbauer Avatar answered Oct 07 '22 19:10

Günter Zöchbauer


For future reference, I solved by using Angular 2's FormBuilder as in:

ts

   ...

   constructor(...
               private formBuilder: FormBuilder);


   timeForm: ControlGroup;

    ngOnInit(){
        let regexPatterns = {
           // this can be improved
           hours: "[0-2]?[0-9]?",
           minutes: "[0-5]?[0-9]?"
        };

        this.timeForm = this.formBuilder.group({
           hour: ['', Validators.pattern(regexPatterns.hours)],
           minute: ['', Validators.pattern(regexPatterns.minutes)]
       });

html

       <form [ngFormModel]="timeForm">
            <!-- additional validation (HTML 5) for 'required' -->
            <input type="text" required ngControl="hour">
            <input type="text" required ngControl="minute">
       </form>
like image 24
dragonmnl Avatar answered Oct 07 '22 18:10

dragonmnl