2

How can I add validation for the HTML5 input type range?

It should not be 0. How can I add a warning message, like the default validation messages on other HTML5 input types?

1

3 Answers 3

1

Example: <input type="number" size="6" name="age" min="18" max="99" value="21">

Some more examples: HTML5 Validation Types

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

1 Comment

The example you gave is input type="number". The question is about input type="range".
1

I'd add the pattern attribute:

pattern="[1-1000]"

That requires a number entered between 1 and 1000

I'd also add:

required="required"

Comments

-1

You can check if the user press the key with an eventlistener via jquery and prevent the use of it

$("input").addEventListener("keypress", function (evt) {
        /*your condition*/
        if ( evt.which == /*your keycode*/ )
        {
            evt.preventDefault();
            alert(); /*you error message/code*/
        }
    });

http://www.webonweboff.com/tips/js/event_key_codes.aspx

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.