5

I'd like to validate an input field within a preconfigured range of numbers. Only numbers from 1 up to 24 are allowed.

How can I do?


With a custom solution like this:

    $('.field').keypress(function(event) {
        var val = parseInt($(this).val()) + HERE_I_SHOULD_SUM_THE_NEWLY_ADDED_DIGIT;
        if(val < 1 || val > 24) event.preventDefault();
    });

I'm able to retrieve the current input value. How can I add the newly added digit?

5 Answers 5

11

How about something like this?

​$("input").on("change", function() {
    var val = Math.abs(parseInt(this.value, 10) || 1);
    this.value = val > 25 ? 24 : val;
});​​​​​​​​​​​​​

DEMO: http://jsfiddle.net/3jw4S/

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

3 Comments

@MichDart What do you mean by doesn't work? Have you checked the demo? Do you consider that change event runs only when input element loses focus? If you need immediate reaction, then try keyup event: jsfiddle.net/3jw4S/1.
Yes, It works when the input loses focus. Is it possible to change this behaviour to handle a keypress event? thanks
Perfect! Just a little fix to this line: $(this).val(val > 24 ? 24 : val);
2

There are builtin functionalities for inputs in modern browsers:

A comprehensive overview can be found here: http://www.the-art-of-web.com/html/html5-form-validation/

Basically, you could write:

<input type="number" min="1" max="24" step="1" required>

with pattern you can define regular expressions to test against:

pattern="\d[0-3]"

Example

3 Comments

I know, but I need some fallbacks
@MichDart then define these attributes and evaluate them with javascript if the browser can't handle it. Thats the best solution.
@MichDart i updated the fiddle, this should be the way to go to be compatible to medern browser without leaving the old ones behind.
1

There's a plugin for JQuery that can help you with validation called Valid8:

http://unwrongest.com/projects/valid8/

You will need to use a regular expression such as this one:

^([1-9]|[1]?[1-9]?|[2][0-4]|10)$

4 Comments

And this is true for 0... sorry ;)
That's also a great solution. However, I need a more immediate reaction.
I'm really not meaning to be a dick but the current edit returns false for 10. For a numeric range its probably easier to cast the value to an integer for numeric comparison.
Thanks. I haven't used regular expressions in years so I'm trying to get back in the game.
0
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x <1|| x >24)
  {
  alert("Value must be between 1 & 24");
  return false;
  }
}

Something along those lines should work

1 Comment

I cannot submit the form. I'm working with ajax. Is it possible to check for validation during input insertion?
0

Try this

function validate() {
 var x = parseInt(value,10) // its the value from the input box;
 if(isNaN(x)||x<1||x>24)
 {
  alert("Value must be between 1 and 24");
  return false;
  }
}

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.