0

I need code that changes input value if its above max or belove min number. For example min is 1 and max 10. If input is set to 15 then it changes to 10 and if its set to 0 then it changes to 1. And it should work without reloading page.

1
  • 2
    Have you tried anything? If you are using HTML5, <input type="number" min="1" max="10"/> should work. Commented Feb 24, 2015 at 15:39

2 Answers 2

1

Use the min and max attributes of the number-typed input element:

<input type="number" min="1" max="10" placeholder="Enter a number between 1 and 10" />

The form will prevent the user from entering values outside of the range, without any coding.

If you want your input to support decimals, add the step="any" attribute.

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

Comments

0

Use as

Here's a simple function that does what you need:

<script type="text/javascript">
function minmax(value, min, max) 
{
    if(parseInt(value) < 0 || isNaN(value)) 
        return 1; 
    else if(parseInt(value) > 10) 
        return 10; 
    else return value;
}
</script>
<input type="text" name="textWeight" id="txtWeight" maxlength="5" onkeyup="this.value = minmax(this.value, 0, 10)"/>

If the input is not numeric it replaces is with 1

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.