36

I am working on a Chrome Packaged App so my code should only work in Chrome.

I have the following input

<input type="date" />

https://jsfiddle.net/jhbo4q2k/

On Chrome this automatically adds a DatePicker. I would like to only keep this Datepicker and disable the input by keyboard.

Is this possible?

EDIT:

The accepted answer works. Just be wary of this

https://developer.chrome.com/extensions/tut_migration_to_manifest_v2#inline_scripts

You cant use inline scripts in a packaged app.

b-m-f
  • 1,278
  • 2
  • 13
  • 29

8 Answers8

97

You can use onkeydown and prevent user from entering the value.

<input type="date" onkeydown="return false" />
mohamedrias
  • 18,326
  • 2
  • 38
  • 47
36

For ReactJS above solutions don't work

I had to do:

<input type="date" onKeyDown={(e) => e.preventDefault()} .... />
4

Hi you can prevent keyboard popup by using onfocus="blur()". Since when the element has the focus we will remove the focus out of it(native keyboards won't show) however with onclick we can continue with our operations.

 <input type="date" class="form-control"  onfocus="blur()" onclick="dosomework()" name="some-name" id="some-id" >
<script>

function dosomework(){
 alert('hi');
 }

<script>
0

If using django forms;

datetime = forms.DateTimeField(widget=forms.DateTimeInput(attrs={
            'onkeydown': 'return false' # If you want to disable the keyboard
            "onfocus": 'blur()' # If you also want to disable virtual keyboard(on smartphones).
        }))

OR

document.getElementById('id_datetime').onkeydown = (e) => {
    e.preventDefault();
}
document.getElementById('id_datetime').setAttribute('onfocus', 'blur()');

Django simply adds 'id' in front of the input field name and sets that as its id. Here the input field name is datetime, so the id will be id_datetime.

Irfan wani
  • 4,084
  • 2
  • 19
  • 34
0

e.preventDefault() will do the job...

const inputDate = document.querySelector("input");

inputDate.addEventListener("keydown", function (e) {
  e.preventDefault();
});
<input type="date">
Minhaz
  • 114
  • 2
  • 8
0

If you are using third party libraies for controlling the input like react-hook-form means you have to include the onKeyDown on form control also then only you will prevent the onKeyDown on input type date.

    <Controller
                    onKeyDown={(e) => e.preventDefault()}
                    name="date"
                    control={control}
                    render={({ field, value }) => (
                        <Form.Control
                            value={value}
                            {...field}
                            onKeyDown={(e) => e.preventDefault()}
                            type="date"
                            id="date"
                            className="formcontrol"
                            placeholder="Pick the date"
                            
                        />
                    )}
                />
-1

Easy Way To Enable & Disable Input Date:

Step 1: create input date.

<input type="date" id="dateEnd">

Step 2: create enable and disable button

<button onclick="disableBtn()">Disable Date Field</button>
<button onclick="undisableBtn()">Undisable Date Field</button>

Step 3: Javascript for enabling and disabling

<script>
  function disableBtn() {
    document.getElementById("dateEnd").disabled = true;
  }

  function undisableBtn() {
    document.getElementById("dateEnd").disabled = false;
  }
</script>

This is the complete example:

<input type="date" id="dateEnd">

<button onclick="disableBtn()">Disable Date Field</button>
<button onclick="undisableBtn()">Undisable Date Field</button>

<script>
  function disableBtn() {
    document.getElementById("dateEnd").disabled = true;
  }

  function undisableBtn() {
    document.getElementById("dateEnd").disabled = false;
  }
</script>

Hope, this may help you.

Viraj Doshi
  • 771
  • 6
  • 26
Muhammad Ali
  • 956
  • 3
  • 15
  • 20
-1

Disabling keyboard input is a great way to make your application less accessible to people with disabilities and make it less user friendly in general, in my opinion keyboard input is much easier to use than date pickers, especially if the expected format is clear. You'd be better off doing some basic field validation to ensure that the input is sensible before letting it be submitted, the HTML date field already has strong validation on it by default on Chrome Firefox and Edge.

Marko
  • 1
  • Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 01 '21 at 14:55
  • This is true if you are making the application for yourself, this goes out the window the second you start making it FOR someone, because their mandated requirements trump ease of use, ease of design, or even improbability of operation. – easleyfixed Nov 09 '22 at 17:47