Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input only accept 0-9(Number in English) and ০-৯ (Number in Bengali)?

I have a html input field . I want to allow only [0-9] (digits in English) and [০-৯] (digits in Bengali)

If I used type='number' then it only support [0-9] not support [০-৯].

If I used type='text' then it support any type of character

Any Idea please?

like image 248
Helal Uddin Avatar asked Nov 21 '19 07:11

Helal Uddin


People also ask

How do I make input only accept numbers in HTML?

You can use the <input> tag with attribute type='number'. This input field allows only numerical values. You can also specify the minimum value and maximum value that should be accepted by this field.

How do I restrict a number in a text box in HTML?

The standard solution to restrict a user to enter only numeric values is to use <input> elements of type number. It has built-in validation to reject non-numerical values.

How do you check if input is number in HTML?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.


2 Answers

If you want to completely disable the user from inserting not numeric digits, then you should overwrite the keydown event and prevent it if the key isn't right.
You can also add an event for paste to only allow paste events that only contain the allowed characters.

var allowedKeys = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
  "ARROWRIGHT", "ARROWLEFT", "TAB", "BACKSPACE", "DELETE", "HOME", "END", "০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"
];

if (document.readyState === "complete") onLoad();
else addEventListener("load", onLoad);

function onLoad() {
  document.querySelectorAll("input.numeric").forEach(function(input) {
    input.addEventListener("keydown", onInputKeyDown);
    input.addEventListener("paste", onInputPaste);
  });
}

function onInputKeyDown(event) {
  if (!allowedKeys.includes(event.key.toUpperCase()) && !event.ctrlKey) {
    event.preventDefault();
  }
}

function onInputPaste(event) {
  var clipboardData = event.clipboardData || window.clipboardData;
  if (/^[0-9০-৯]+$/g.test(clipboardData.getData("Text"))) return;
  event.stopPropagation();
  event.preventDefault();
}
<input type="text" class="numeric">
like image 86
nick zoum Avatar answered Oct 17 '22 19:10

nick zoum


Here is a working pure HTML-CSS solution. Please note that text can still be entered into the input element but the browser can handle the validation.

It is a trade-off you need to make to support multilingual-input box.

input:invalid {
  border: 2px dashed red;
}

input:valid {
  border: 2px solid black;
}
<form>
  <label for="numberBox">Enter a number</label>
  <input type="text" name="numberBox" pattern="[০-৯0-9]" title="Numbers only">
  <button>Submit</button>
</form>
like image 28
Narbhakshi Avatar answered Oct 17 '22 18:10

Narbhakshi