1

When using type="number" on an input field, regex validation does not appear to work.

<input type="number" step="any" min="0" max="24" value="0">

The new broswer based validation using step, min, max works as expected. However this is not consistent accross browsers?

http://jsfiddle.net/EkL3k/1/


QUESTION

How to make a number field validate using regular expression?

I'm also interested in other factors that differentiate a number and text field if anyone has information.

NOTES

I have discovered that checking for an empty string causes validation to fire IF the conditions on the number field are not met.

7
  • You can always call your validation by script on keyup. Or just set remove the type=number and do it with regex only. Commented May 15, 2014 at 9:16
  • Thanks for the tip. Key up makes no difference and I need type number for device keypads. Commented May 15, 2014 at 9:22
  • You can use element.validity.valid to check if the input contains valid number. There is also a pattern attribute in which you can set a regex to validate your input. Finally, you can prevent a user from typing something else than numbers by using keypress : stackoverflow.com/a/15649226/2806497 Commented May 15, 2014 at 9:24
  • BTW, be cautious with those new inputs, they're not fully implemented in all browsers (for example, firefox added support for it in its last update). I would not recommend to use it in a production context. Commented May 15, 2014 at 9:27
  • Exactly which is why I am using regular expression validators to ensure full support. I cannot rely on the number validation built into the browser. Commented May 15, 2014 at 9:28

3 Answers 3

1

A number field performs its own validation, if it contains a non-numerical character, the value will automatically be removed until a correct value is given. You can see this with a console.log(value).

So you could also check for an empty string

function check(value, msg) {
  var valid = ((value != '') && /^\d*\.?\d*$/.test(value));    
  if (valid) {
      document.getElementById(msg).style.display = "none";
  } else {
      document.getElementById(msg).style.display= "inline";
  }
  return valid;
}

http://jsfiddle.net/EkL3k/6/

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

2 Comments

This is true however not all browser behave, validation wont be consistent and regular expressioon validators prevent data submission which I also require.
Checking empty string does cause validation to occur if value is outside number field specified quantities.
1

RegEx does not work because the returned value is a number, not a string. It works 'as expected' when you force the returned value to string format:

var valid = /^\d*\.?\d*$/.test(String(value));

You might want to read How to get the raw value an <input type="number"> field? as it suggests you don't have to validate a type=number input.

2 Comments

This is true however not all browser behave, validation wont be consistent and regular expressioon validators prevent data submission which I also require.
Well it worked for me after changing just this line in your fiddle. ..?
0

add this code and add an id to your input.

$(document).ready(function() {
$("#txtboxToFilter").keydown(function (e) {
    // Allow: backspace, delete, tab, escape, enter and .
    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
         // Allow: Ctrl+A
        (e.keyCode == 65 && e.ctrlKey === true) || 
         // Allow: home, end, left, right
        (e.keyCode >= 35 && e.keyCode <= 39)) {
             // let it happen, don't do anything
             return;
    }
    // Ensure that it is a number and stop the keypress
    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
        e.preventDefault();
    }
});
});

http://codepen.io/anon/pen/IDEGu

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.