38

I like to have an input with maximum 3 characters. In Firefox everything works fine I can only write 3 numbers inside the input - but in safari you can write a lot of numbers inside.

You can test it in both browsers with this: http://flowplayer.org/tools/demos/validator/custom-validators.htm

For now I realized it with a validate plugin - but just for interest I like to know why isn’t this working?

EDIT:

with this it's working

<input class="required" id="field" type="text" maxlength="3" pattern="([0-9]|[0-9]|[0-9])" name="cvv"/>

answer: How can I prevent letters inside a text element?

2
  • What you can use is attribute max and min. See stackoverflow.com/questions/8354975/… Commented Feb 20, 2012 at 12:41
  • that's curious max and min are also not working !? Commented Feb 20, 2012 at 13:13

9 Answers 9

35

I've accomplished it with:

<input class="required" id="field" type="text" maxlength="3" pattern="([0-9]|[0-9]|[0-9])" name="cvv"/>

and then in JavaScript I prevented the letters:

$("#myField").keyup(function() {
    $("#myField").val(this.value.match(/[0-9]*/));
});
Sign up to request clarification or add additional context in comments.

7 Comments

I think you can use [0-9]{3} instead of [0-9][0-9][0-9] in your pattern (I know it works in Chrome at least).
This is nice in iOS but on android the keyboard still shows up as text, not numbers.
@lizlux add the attribute inputmode=numeric
the spec is idiotic, the fact that we need to change from type="number" to type="text" with pattern and javascript is ridiculous
Just as a comment, by using type="text" you loose the ability of displaying the keypad on mobile devices, just a thing to keep in mind if your app is going mobile.
|
26

You may try to user type="text" and oninput as below. This will let the numbers only.

<input type="text" maxlength="3" oninput="this.value=this.value.replace(/[^0-9]/g,'');" id="myId"/>

Comments

6

Using onKeyDown length can be restricted

<input type="number" onKeyDown="if(this.value.length==10 && event.keyCode!=8) return false;">

Happy Coding :)

Comments

5

Basically Max and Min properties are not supported in Safari browser yet. I can see no flaw in the syntax. Are u using the Safari version 1.3+? or below that? Because maxlength property is supported from safari 1.3+.

1 Comment

Thank you :) I'm using Safari 1.5.2. I realized it like beneath:
1

I used something very similar to @Jules answer except his won't allow the use of keys like shift+home. Since we're validating numbers I just used isNaN in the keyup function.

$("#myField").keyup(function() {
    var e = $("#myField");
    if(isNaN(e.val())){
        e.val(e.val().match(/[0-9]*/));
    }
});

With...

<input id="#myField" inputmode="numeric" maxlength="3" pattern="([0-9]{3})" type="text" />

Comments

1

Here is a more simple solution. For me it worked

<input type="number" id="cvv">


$("#cvv").on("keypress", function(evt) {
  var keycode = evt.charCode || evt.keyCode;
  if (keycode == 46 || this.value.length==3) {
    return false;
  }
});

the JS block will prevent the "."(dot) so one cant enter float. We are keeping the input type as number so it will be only number as input. If the value length is 3 then it will return false, so input length also restricted.

Comments

0

This thing worked for me... So basically Safari and Firefox still lets you introduce invalid characters and removes the whole thing on submit.

<input
    onInput={(event) => {
        if(!event.target.validity.valid) event.target.value = 
    this.state.value
    }}
/>

Comments

-1

https://jsfiddle.net/zxqy609v/8/

function fixMaxlength(inputNumber) {
  var maxlength = $(inputNumber).attr("maxlength");
    $(inputNumber).keyup(function (evt) {
    this.value = this.value.substring(0, maxlength);
    this.value = this.value.replace(/\D/g, '');
  });
}

Comments

-2

You can not use maxlength and minlength function with input type=”number”

Instead one can use regexp to solve this problem
(?=.*[0-9]) - must contain only digit

{10,10} - min length and max length must be 10. You can change these values

<input type="number" pattern="(?=.*[0-9]).{10,10}" />

Use

pattern="(?=.*[0-9]).{10,10}"

1 Comment

Chrome is ignoring the regexp, don't know about other browsers.

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.