htmlformsrequired-fieldreadonly-attributeinputtext

how I create a form input text readonly and required at the same time?


I'm reviewing my code wrote time ago. The input text that I wrote is the following:

<input type="text" class="form-control form-control-sm" id="mypid" name="mypid" value="" placeholder="" readonly="readonly" required>

I tested the form and with my surprise I found out that even if I added 'required', I can actually submit even if the field is empty. The field is readonly because my code will autofill that field automatically, I need that the user doesn't write anything by themselves, so I need it to be readonly and required at the same time. But I didn't expect to see that now it's not considered required anymore. What am I doing wrong here? Any suggestion? Thank you very much for your help.

Elliot


Solution

  • your question has already been answered here. But to explain it to you, you have to replace your readonly by onkeypress="return false;" which gives almost the same result.