0

I want an input field in html which will take number input only (including decimal) with increment disabled. I have used following :

/* Hide Spin arrows on input type number */

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  /* display: none; <- Crashes Chrome on hover */
  -webkit-appearance: none;
  margin: 0;
  /* <-- Apparently some margin are still there even though it's hidden */
}

input[type="number"] {
  -moz-appearance: textfield;
}
<input class="form-control" type="number" name="phone">

This works perfectly on Google Chrome with out any problem. But, It doesn't works on Firefox. Does anyone know where I am doing wrong ? Is there any other idea to get out of this problem ?

Thanks,

4
  • I don't get it, how will the css work if it targets input with type="number", but you are using an input with type="text"? Commented Jun 15, 2017 at 6:06
  • How can you select a text field with [type="number"] selector? Commented Jun 15, 2017 at 6:07
  • @Swellar It's my typo error, I have updated Commented Jun 15, 2017 at 6:34
  • @AbhishekPandey it's my typo error I have updated Commented Jun 15, 2017 at 6:34

2 Answers 2

1

Try using

input[type=number] {
    -moz-appearance:textfield;
}

However, using input type='number' will never assure that numeric inputs are given in your input in firefox. you have to write a custom validation function in case of firefox. This is an example.

<!DOCTYPE html>
<html>

<head>
    <style>
        input[type=number]::-webkit-inner-spin-button,
        input[type=number]::-webkit-outer-spin-button {
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            margin: 0;
        }
        input[type=number] {
            -moz-appearance:textfield; /*This is for firefox*/
        }
    </style>
    <script>
        //Custom number validating function
        function isNumber(evt) {
            var iKeyCode = (evt.which) ? evt.which : evt.keyCode
            if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
                return false;

            return true;
        } 
    </script>
</head>

<body>
    <input type="number" name="phone" onkeypress="javascript:return isNumber(event)">
</body>

</html>

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

3 Comments

Arrow icon not showing which is fine but it takes text as well and type number not woking
I'm using laravel framework and there is error due to other issues. Your code works perfectly. Thank you so much
You are Welcome.
0

i dont no why its not working on your firebox browser because of version or any other reasion

this code perfectly work on my both browser firefox and crome

change input type text to number first

<input class="form-control" type="number" name="phone">

and your css code

<style type="text/css">
/* Hide Spin arrows on input type number */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    /* display: none; <- Crashes Chrome on hover */
    -webkit-appearance: none;

    margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}
input[type="number"] {
    -moz-appearance: textfield;
}
</style>

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.