926

In Google Chrome some customers are not able to proceed to my payment page. When trying to submit a form I get this error:

An invalid form control with name='' is not focusable.

This is from the JavaScript console.

I read that the problem could be due to hidden fields having the required attribute. Now the problem is that we are using .net webforms required field validators, and not the html5 required attribute.

It seems random who gets this error. Is there anyone who knows a solution for this?

mp1990
  • 9,699
  • 3
  • 13
  • 17
  • 16
    If you think that it might be due to hidden required fields, maybe you should check if in some cases those fields remain blank? For example, if you fill in user_id from session, or something similar, in certain cases it might remain blank? – dkasipovic Mar 03 '14 at 13:31
  • 2
    Agreed. Open the developer console (F12) in Google Chrome and examine the values of the fields to see if the values for these fields are blank. – The Vanilla Thrilla Mar 03 '14 at 13:33
  • Thank you for your comment. It doesnt make any sence to me though? I have a simple asp.net form, which uses asp.net form controls. The input controls is generated by the framework. – mp1990 Mar 03 '14 at 13:35
  • I checked and there are values in the input fields. I have one hidden input field which also has a value. – mp1990 Mar 03 '14 at 13:36
  • The funny thing is, i deployed to our production environment this afternoon where the error suddenly began, i tried to roll back the files but the error now keeps occuring. You can see it by going to bentbrandt.dk and try doing a search. Some get the error in Chrome and some dont. I dont. – mp1990 Mar 03 '14 at 13:38
  • Duplicate of ["Invalid form control" only in Google Chrome](http://stackoverflow.com/questions/7168645/invalid-form-control-only-in-google-chrome) – Dan Dascalescu Oct 24 '14 at 08:30
  • It shows if there is an input without 'name' attribute. Adding name to input solved the issue for me – Vildan Dec 28 '15 at 22:20
  • 7
    I think It happens when the field is required but is not "visible" in general terms. It happen to me in a form splitted into several tabs the field who triggered the error is in a tab that is not active at the moment of submit I removed required attribute from field and did specific validation – TexWiller Dec 07 '19 at 10:27
  • In my case required="" creates error. I remove it and it works fine. You have to find required keyword in your code. – chirag p Oct 27 '21 at 10:53
  • Hi , In my case it is due to invalid Value from DB for one particular form:input field. Is there any way to cancel the validation only for the .hide() fields ? – rinilnath Nov 23 '21 at 13:56

45 Answers45

1114

This issue occurs on Chrome if a form field fails validation, but due to the respective invalid control not being focusable the browser's attempt to display the message "Please fill out this field" next to it fails as well.

A form control may not be focusable at the time validation is triggered for several reasons. The two scenarios described below are the most prominent causes:

  • The field is irrelevant according to the current context of the business logic. In such a scenario, the respective control should be disabled or removed from the DOM or not be marked with the required attribute at that point.

  • Premature validation may occur due to a user pressing ENTER key on an input. Or a user clicking on a button/input control in the form which has not defined the type attribute of the control correctly. If the type attribute of a button is not set to button, Chrome (or any other browser for that matter) performs a validation each time the button is clicked because submit is the default value of a button's type attribute.

To solve the problem, if you have a button on your page that does something else other than submit or reset, always remember to do this: <button type="button">.

Igwe Kalu
  • 14,286
  • 2
  • 29
  • 39
  • 2
    This problem may arise when a page is built & tested with a browser that doesn't support clientside validation or doesn't prevent the form submission (see Safari). The next developer or user with a browser that actually prevents form submission on clientside errors (see Chrome; even on hidden form elements) runs into the problem of an inaccessible form as one can't submit the form and doesn't get any message from the browser or site. Preventing validation altogether via 'novalidate' is no correct answer as clientside validation should support user inputs. Changing the website is the solution. – graste May 07 '15 at 07:53
  • 10
    Thanks for the detailed answer. Omitting `required` or adding `novalidate` is never a good workaround. – fatCop May 31 '15 at 11:20
  • Good answer. Adding novalidate worked fine for me as I'm using Angular form validation which is still applied. – NathofGod Jul 16 '15 at 15:12
  • ` – Mohammad Kermani Aug 02 '16 at 06:25
  • "Chrome performs a validation each time the button is clicked" because the type defaults to `submit`. – Knu Aug 10 '16 at 21:57
  • 2
    This should be the correct answer. If you are using html5 client side validation, adding novalidate fixes one problem while creating another. To bad Chrome wants to validate a input that isn't shown (and thus shouldn't be required). – kheit Sep 26 '16 at 20:23
  • 4
    Just leaving en example in my case, had a hidden `number` field where the default value wasn't dividable evenly by the step size. Changing the step size solved the issue. – James Scott Oct 06 '16 at 08:10
  • 5
    @IgweKalu good call. I ran into this implementing TinyMCE over a textarea field. TinyMCE hides the original form field (which was "required") and triggers this error. Solution, remove the now hidden "required" attribute from the form and rely on other method to ensure it is filled in. – john Apr 14 '17 at 03:43
  • 1
    Thanks a lot Guys, it worked for me & saved my time by using type="button". – Manveer Singh May 02 '17 at 11:08
  • Oh my gosh, that whole "remove required" really pin-pointed the problem. Recently I added that element oblivious as to what it does while trying to troubleshoot another issue. I've fixed the preceding issue and by such thought nothing of the added "required" tag, but when it came time to do an actual button submission, chrome threw that error. Thank you so much for posting a detailed answer. This should be the acceptable response! – Lukas Mar 16 '18 at 18:47
  • You dont need to change the submit button to a button, a javascript onclick action (jquery or otherwise) to make those fields show or display will get rid of those messages – PBo May 15 '19 at 14:12
  • 1
    I am using a step form with validation and clicking next triggered this error. This solution works in this case too. – Monduiz Sep 28 '19 at 12:34
  • Can anyone explain why `type="button"` solves this issue? – 71GA Jan 15 '20 at 21:21
  • 1
    @71GA If the type attribute is not set, then it’s set to `type='submit'` by default. Hence the button may trigger field validation before your application would intend to do so. – Igwe Kalu Jan 16 '20 at 08:34
  • 1
    I found that even though the `input` was not `required`, I needed to set its value to `''` before form submission happened. This scenario might occur if the user interacts with the input / fills it before its hidden. And because it has a value, Chrome might try to validate it (I was using `type="number"`). – pradeepcep Jun 25 '20 at 16:16
  • It's also possible you have multiple `button type="submit"` in your form/on your page, like I had, due to copy-pasting. – kasimir May 17 '21 at 18:51
  • In my case required="" creates problem. I removed it and works fine. – chirag p Oct 27 '21 at 10:52
  • Excellent explanation. In my case it's because I have a custom checkbox which hides the original one. As the required element was not generating a message to the user, I removed it and put the initial validation into javascript. – zsalya Jul 29 '22 at 22:17
  • In my case I had to set `step="any"` to allow for decimal numbers before this error went away. By default `step` is set to "1" and only integers are allowed. – Luka Peharda Apr 24 '23 at 08:28
426

Adding a novalidate attribute to the form will help:

<form name="myform" novalidate>
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user2909164
  • 4,677
  • 1
  • 12
  • 2
  • 2
    Thank you for your comment. But i wonder what security risks would come with that? If any? Or does it simply tell the browser not to validate the input fields inside the form? – mp1990 Mar 03 '14 at 14:06
  • I tried your suggestion and everything works now as it should, however i am in doubt i will go with this if this is a security risk. – mp1990 Mar 03 '14 at 14:12
  • 59
    It’s not a security risk. (Relying on client-side checks is.) It’s a usability problem. – Jukka K. Korpela Mar 03 '14 at 14:39
  • 34
    I tried and found that "novalidate" is indeed required. Only adding a form name does not work. – Jeff Tian Aug 27 '14 at 09:03
  • 1
    I met this problem with Angular JS form submitting, and adding "novalidate" solved this issue. – Jeff Tian Aug 27 '14 at 09:04
  • 15
    Adding novalidate also stops client side validation, such as with the Rails Simple Form gem. Better to make the problem fields not required, per Ankit's answer + David Brunow's comment. – steel Nov 17 '14 at 17:18
  • 1
    Thanks, this helped me realize that my form didn't have a name but an id. So i added the name="form-name" attribute and now it works fine. I don't get that error anymore ;) – Pathros Jun 02 '15 at 15:25
  • 2
    This is just disabling all clientside validation for the form. I don't think this is what you wanted to do. – superluminary Jun 05 '15 at 16:16
  • 9
    I discovered that "An invalid form control with name='' is not focusable." also shows if the inputs are in a `
    `
    – oscargilfc Jun 10 '15 at 02:13
  • The solution by @Pere is also a valid scenario and it worked in my case. hope it helps someone as we shall be able to solve the issue without losing the power of validation – Ram Aug 29 '15 at 14:00
  • 1
    This will prevent any validation on the form. Instead I want my code to highlight required field in browser, not just complain in console. – ZurabWeb Sep 04 '15 at 19:48
  • Are you using some kind of custom checkbox? Those can't be highlighted. Either way, you can direct your users with a JavaScript notification using an alert package of some sort. – Quin Nov 08 '15 at 17:44
  • novalidate worked for me. The error is annoying but doesn't impact the functionality in my case. – techwestcoastsfosea Dec 11 '15 at 05:11
  • novalidate is disabling fields with required="true", this impact my forms. – Mimouni Jun 07 '16 at 11:13
  • This worked well, but my form is visible, not hidden. And now the content of the form/textarea is empty; `""` on server side. – P_95 Aug 11 '16 at 07:02
  • this is a terrible suggestion to remove. You lose client side validation, which while should not be the only type of validation, enhances the user experience since you don't have to rely on the server to do work. – alex Mar 18 '17 at 22:06
  • @alex You should ALWAYS rely on your server for validation. Client side validation is for user-experience only. NEVER rely on it solely. Client-side validation should not decrease the workload on the server what-so-ever. If it does, you're doing it wrong. :) – Native Coder Apr 12 '17 at 18:34
  • 2
    @NativeCoder - If you need to validate a phone number is in a certain regex, you will need to postback to the server in this scenario. That adds overhead and takes more time than a quick JS regex check (decreases user experience). You can have the same regex check on the client side to pre-validate and then on your server you do final validation (which should always be in place, hence my saying client side should not be the only type of validation). – alex Apr 12 '17 at 18:41
  • 2
    @alex Then we are saying the same thing. Always validate server-side not matter what. That way WHEN someone tries to hack around and break your client side validation, you are covered. – Native Coder Apr 12 '17 at 21:11
  • The problem in my case was i forgot to close the tag , and that was covering another form below it. Adding the closing tag appropriately fixed it for me. – FreeKrishna Apr 18 '17 at 12:44
  • 1
    A good explanation on how html form validation works and why does the novalidate stop this error to be triggered would have been so much useful for so many people :/ Too bad answers are accepted to fast – Alburkerk Jun 27 '17 at 07:27
332

In your form, You might have hidden input having required attribute:

  • <input type="hidden" required />
  • <input type="file" required style="display: none;"/>

The form can't focus on those elements, you have to remove required from all hidden inputs, or implement a validation function in javascript to handle them if you really require a hidden input.

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
Ankit Sharma
  • 5,191
  • 2
  • 23
  • 29
  • 8
    I noticed that this can also happen with fields that are of type "email" set to display none, the problem caused by Chrome trying to validate the format. – David Brunow May 30 '14 at 05:20
  • 3
    It doesn't need to have `required`. `pattern` alone can cause this problem. – Pacerier Jan 27 '15 at 06:24
  • 8
    In my case, the form fields were not type="hidden" they were simply out of view, eg in a form with tabs. – Josh Mc Feb 09 '15 at 01:31
  • I have two forms, one with both an `` and another with an ``, and the only that is displaying the error in Chrome is the `display: none`, while the `"hidden"` one submits fine. – Pere May 13 '15 at 14:19
  • This is the correct answer. Chrome is attempting to validate a field which has been hidden in CSS. The is preventing it from showing its validation warning message, and it's alerting you of this fact. – superluminary Jun 08 '15 at 09:10
  • In my case the input control is inside a div and div is hidden using ng-show. I had to remove the required attribute and fix the validation rules for the hidden control. This answer certainly helped me. – Anil Vangari Feb 17 '16 at 16:35
  • In my case this fixed my issue. – Taha Khan Sep 25 '21 at 11:52
43

In case anyone else has this issue, I experienced the same thing. As discussed in the comments, it was due to the browser attempting to validate hidden fields. It was finding empty fields in the form and trying to focus on them, but because they were set to display:none;, it couldn't. Hence the error.

I was able to solve it by using something similar to this:

$("body").on("submit", ".myForm", function(evt) {

    // Disable things that we don't want to validate.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", true);

    // If HTML5 Validation is available let it run. Otherwise prevent default.
    if (this.el.checkValidity && !this.el.checkValidity()) {
        // Re-enable things that we previously disabled.
        $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);
        return true;
    }
    evt.preventDefault();

    // Re-enable things that we previously disabled.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);

    // Whatever other form processing stuff goes here.
});

Also, this is possibly a duplicate of "Invalid form control" only in Google Chrome

Community
  • 1
  • 1
Horatio Alderaan
  • 3,264
  • 2
  • 24
  • 30
  • 8
    If you feel inclined to vote this answer down, please leave a comment to let me know why. I would rather remove the answer if it contains bad information than leave it here and mislead the community. – Horatio Alderaan Jun 02 '14 at 04:05
  • 2
    I won't down vote, but feel this code uses a brute-force "masking the problem" approach. It certainly has usefulness when dealing with legacy code, but for newer projects I would recommend looking at your problem a little harder. This answer also applies to using "novalidate" on the form tag, but that's a little more visible. +1 for disabled instead of removing required. – Jonathan DeMarks Oct 27 '14 at 14:58
  • 1
    To give a little bit of context, I was in exactly in the situation of "dealing with legacy code" when I ran into this problem. I fully agree that it's a hack. Much better to design the markup so that you don't encounter this situation. However, with legacy codebases and inflexible CMS systems, this is not always possible. The approach above of disabling rather than using "novalidate" was the best I way I could think of to solve the problem. – Horatio Alderaan Nov 11 '14 at 23:02
  • This doesn't work for me, because the javascript throws an error before the submit function gets called. – Marcel Burkhard Jan 08 '15 at 13:29
  • 3
    I agree with the approach of disabling hidden fields. Removing their 'required' attributes loses the information should they be shown again later. Re-enabling them would keep this, however. – Druckles Jun 04 '15 at 16:38
  • 1
    I love this solution, and think it works nicely when you have a form with inputs that are variably displayed or hidden based on the selected values of other inputs. This preserves the required settings, and still allows the form to submit. Even better, if validation fails and the user does something completely different, there are no additional resets needed. – lewsid Aug 17 '17 at 01:42
32

In my case the problem was with the input type="radio" required being hidden with:

visibility: hidden;

This error message will also show if the required input type radio or checkbox has a display: none; CSS property.

If you want to create custom radio/checkbox inputs where they must be hidden from the UI and still keep the required attribute, you should instead use the:

opacity: 0; CSS property

Gus
  • 6,545
  • 4
  • 36
  • 39
  • same issue with exception that i am using selectivity.js library which hide the current select box and show an alternative js box with js and css. when i use required on it throws this error form-field.js:8 create:1 An invalid form control with name='listing_id' is not focusable. cny sugestion how can i work it arround. – Inzmam ul Hassan Jul 24 '17 at 11:19
  • @InzmamGujjar as I understood, your js plugin is creating another select input with its own classes. Maybe altering the plugin CSS classes or selecting the created input `name=""` instead of the original one? – Gus Jul 24 '17 at 20:05
  • 1
    Helpful answer. I would add if you want to preserve the same layout/styling as display: none, add a position: absolute. Display: none removes the element AND the space it takes up on the page, but visibility: hidden only hides the element visually. – colemerrick Apr 21 '21 at 19:54
  • @Gus - Wonderful solution, this worked for me! – Reena Verma May 28 '21 at 09:06
  • Don't forget `pointer-events: none;` as well to make sure the invisible element doesn't steal clicks away from any elements that might end up stacked below it. You can also fine tune the position of the validation popup so it aligns up with your custom replacement: `transform: translate(2rem, 1.5rem);` – Besworks May 19 '22 at 21:31
19

Not only required field as mentioned in other answers. Its also caused by placing an <input> field in a hidden <div> which holds an invalid value.

Consider below example,

<div style="display:none;">
   <input type="number" name="some" min="1" max="50" value="0">
</div> 

This throws the same error. So make sure the <input> fields inside hidden <div> doesnt hold any invalid value.

Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
18

This issue occurs when you provide style="display: none;" and required attribute to the input field, and there will be validation on submit. for example:

<input type="text" name="name" id="name" style="display: none;" required>

This issue can be resolved by removing required attribute from the input field from your HTML. If you need to add required attribute, add it dynamically. If you are using JQuery, use below code:

$("input").prop('required',true);

If you need to remove this field dynamically,

$("input").prop('required',false);

You can also make use of plain Javascript if you are not using JQuery:

document.getElementById('element_id').removeAttribute('required');
Ayesha
  • 251
  • 2
  • 7
17

None of the previous answers worked for me, and I don't have any hidden fields with the required attribute.

In my case, the problem was caused by having a <form> and then a <fieldset> as its first child, which holds the <input> with the required attribute. Removing the <fieldset> solved the problem. Or you can wrap your form with it; it is allowed by HTML5.

I'm on Windows 7 x64, Chrome version 43.0.2357.130 m.

Pere
  • 1,068
  • 12
  • 20
  • Had the same problem, thanks for the hint. Managed to solve it with your solution, it is odd. – Ben Jul 23 '15 at 02:29
  • Thank you, this was the problem in my case as well. – fantasticrice Jul 24 '15 at 18:44
  • in my case this hint help me – Anja Ishmukhametova Jul 27 '15 at 05:15
  • 2
    Fieldsets were my problem, as well. In my case, the answer was found here: http://stackoverflow.com/a/30785150/1002047 Appears to be a Chrome bug (since the HTML in question validates using W3C's validator). Simply changing my fieldsets to divs solved the problem. – Eric S. Bullington Jul 30 '15 at 14:24
  • @EricS.Bullington thanks for pointing out. I didn't know of that answer; as that user, I found it by myself "slicing" out pieces of my form. By the way, I don't know about Chrome version numbering system, but from 124 to 130 in 4 years doesn't look like a huge step... – Pere Jul 30 '15 at 17:07
  • This is my problem, But I'm using fieldsets for my form UI. (Literally, splitting the form up into data sets. Like address, personal info, misc, etc). So cutting out the fieldset isn't really an option for me. Does anyone know of another workaround? – Native Coder Apr 12 '17 at 18:38
  • Hi , In my case it is due to invalid Value from DB for one particular form:input field. Is there any way to cancel the validation only for the .hide() fields ? – rinilnath Nov 23 '21 at 13:56
  • @rinilnath I think what you should do is to check the values you get from DB to fill in the fields and assure that they are valid HTML – Pere Nov 24 '21 at 14:24
  • 1
    @Pere, I corrected the issue with another way, the answer by "abhinav kinagi" below provided the clue, there were other fields that are having validations that further caused the same error (unfocussable error). Hence i had to remove the validation attributes during hide() and add it again while show() in Jquery. – rinilnath Dec 02 '21 at 07:54
  • @rinilnath situations like these make me question sometimes about the usefulness of HTML validation nowadays... – Pere Dec 02 '21 at 10:50
12

Yet another possibility if you're getting the error on a checkbox input. If your checkboxes use custom CSS which hides the default and replaces it with some other styling, this will also trigger the not focusable error in Chrome on validation error.

I found this in my stylesheet:

input[type="checkbox"] {
    visibility: hidden;
}

Simple fix was to replace it with this:

input[type="checkbox"] {
    opacity: 0;
}
Sam
  • 4,994
  • 4
  • 30
  • 37
  • This was also the solution that I found to work best. It allows the error message to actually display near the invisible form controls. – Rob Feb 01 '17 at 20:33
11

It can be that you have hidden (display: none) fields with the required attribute.

Please check all required fields are visible to the user :)

ghuroo
  • 318
  • 2
  • 10
10

For me this happens, when there's a <select> field with pre-selected option with value of '':

<select name="foo" required="required">
    <option value="" selected="selected">Select something</option>
    <option value="bar">Bar</option>
    <option value="baz">Baz</option>
</select>

Unfortunately it's the only cross-browser solution for a placeholder (How do I make a placeholder for a 'select' box?).

The issue comes up on Chrome 43.0.2357.124.

Community
  • 1
  • 1
piotr_cz
  • 8,755
  • 2
  • 30
  • 25
10

For Select2 Jquery problem

The problem is due to the HTML5 validation cannot focus a hidden invalid element. I came across this issue when I was dealing with jQuery Select2 plugin.

Solution You could inject an event listener on and 'invalid' event of every element of a form so that you can manipulate just before the HTML5 validate event.

$('form select').each(function(i){
this.addEventListener('invalid', function(e){            
        var _s2Id = 's2id_'+e.target.id; //s2 autosuggest html ul li element id
        var _posS2 = $('#'+_s2Id).position();
        //get the current position of respective select2
        $('#'+_s2Id+' ul').addClass('_invalid'); //add this class with border:1px solid red;
        //this will reposition the hidden select2 just behind the actual select2 autosuggest field with z-index = -1
        $('#'+e.target.id).attr('style','display:block !important;position:absolute;z-index:-1;top:'+(_posS2.top-$('#'+_s2Id).outerHeight()-24)+'px;left:'+(_posS2.left-($('#'+_s2Id).width()/2))+'px;');
        /*
        //Adjust the left and top position accordingly 
        */
        //remove invalid class after 3 seconds
        setTimeout(function(){
            $('#'+_s2Id+' ul').removeClass('_invalid');
        },3000);            
        return true;
}, false);          
});
SudarP
  • 906
  • 10
  • 12
8

If you have any field with required attribute which is not visible during the form submission, this error will be thrown. You just remove the required attribute when your try to hide that field. You can add the required attribute in case if you want to show the field again. By this way, your validation will not be compromised and at the same time, the error will not be thrown.

maniempire
  • 791
  • 11
  • 12
  • **Hint:** `$("input").attr("required", "true");` or `$("input").prop('required',true);` contrary you can `remove` the _attribute_ / _property_. >> [jQuery add required to input fields](https://stackoverflow.com/q/19166685/5737774) – fWd82 Mar 15 '18 at 13:17
8

It happens if you hide an input element which has a required attribute.

Instead of using display:none you can use opacity: 0;

I also had to use some CSS rules (like position:absolute) to position my element perfectly.

Serdar D.
  • 3,055
  • 1
  • 30
  • 23
7

It's weird how everyone is suggesting to remove the validation, while validation exists for a reason... Anyways, here's what you can do if you're using a custom control, and want to maintain the validation:

1st step. Remove display none from the input, so the input becomes focusable

.input[required], .textarea[required] {
    display: inline-block !important;
    height: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    z-index: -1 !important;
    position: absolute !important;
}

2nd step. Add invalid event handler on the input to for specific cases if the style isn't enough

inputEl.addEventListener('invalid', function(e){
   //if it's valid, cancel the event
   if(e.target.value) {
       e.preventDefault();
   }
}); 
Inc33
  • 1,747
  • 1
  • 20
  • 26
5

I came here to answer that I had triggered this issue myself based on NOT closing the </form> tag AND having multiple forms on the same page. The first form will extend to include seeking validation on form inputs from elsewhere. Because THOSE forms are hidden, they triggered the error.

so for instance:

<form method="POST" name='register' action="#handler">


<input type="email" name="email"/>
<input type="text" name="message" />
<input type="date" name="date" />

<form method="POST" name='register' action="#register">
<input type="text" name="userId" />
<input type="password" name="password" />
<input type="password" name="confirm" />

</form>

Triggers

An invalid form control with name='userId' is not focusable.

An invalid form control with name='password' is not focusable.

An invalid form control with name='confirm' is not focusable.

Community
  • 1
  • 1
Frankenmint
  • 1,570
  • 3
  • 18
  • 33
5

There are many ways to fix this like

  1. Add novalidate to your form but its totally wrong as it will remove form validation which will lead to wrong information entered by the users.

<form action="...." class="payment-details" method="post" novalidate>

  1. Use can remove the required attribute from required fields which is also wrong as it will remove form validation once again.

    Instead of this:

<input class="form-control" id="id_line1" maxlength="255" name="line1" placeholder="First line of address" type="text" required="required">

   Use this:

<input class="form-control" id="id_line1" maxlength="255" name="line1" placeholder="First line of address" type="text">

  1. Use can disable the required fields when you are not going to submit the form instead of doing some other option. This is the recommended solution in my opinion.

    like:

<input class="form-control" id="id_line1" maxlength="255" name="line1" placeholder="First line of address" type="text" disabled="disabled">

or disable it through javascript / jquery code dependes upon your scenario.

Umar Asghar
  • 3,808
  • 1
  • 36
  • 32
  • The point #3 is the most relevant solution I would say. I'm surprised that this isn't ranking higher here. – Stepan J. Jun 13 '23 at 13:09
4

It will show that message if you have code like this:

<form>
  <div style="display: none;">
    <input name="test" type="text" required/>
  </div>

  <input type="submit"/>
</form>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Robert
  • 2,342
  • 2
  • 24
  • 41
4

Yea.. If a hidden form control has required field then it shows this error. One solution would be to disable this form control. This is because usually if you are hiding a form control it is because you are not concerned with its value. So this form control name value pair wont be sent while submitting the form.

Kartik Shenoy
  • 308
  • 3
  • 14
  • 1
    Thanks for this idea! It was useful to me to toggle ALL sometimes-hidden form elements regardless of whether the individual ones are required or not, so my logic is nice and simple :) – Gabe Kopley Aug 22 '16 at 23:29
4

Another possible cause and not covered in all previous answers when you have a normal form with required fields and you submit the form then hide it directly after submission (with javascript) giving no time for validation functionality to work.

The validation functionality will try to focus on the required field and show the error validation message but the field has already been hidden, so "An invalid form control with name='' is not focusable." appears!

Edit:

To handle this case simply add the following condition inside your submit handler

submitHandler() {
    const form = document.body.querySelector('#formId');

    // Fix issue with html5 validation
    if (form.checkValidity && !form.checkValidity()) {
      return;
    }

    // Submit and hide form safely
  }

Edit: Explanation

Supposing you're hiding the form on submission, this code guarantees that the form/fields will not be hidden until form become valid. So, if a field is not valid, the browser can focus on it with no problems as this field is still displayed.

Mouneer
  • 12,827
  • 2
  • 35
  • 45
3

You may try .removeAttribute("required") for those elements which are hidden at the time of window load. as it is quite probable that the element in question is marked hidden due to javascript (tabbed forms)

e.g.

if(document.getElementById('hidden_field_choice_selector_parent_element'.value==true){
    document.getElementById('hidden_field').removeAttribute("required");        
}

This should do the task.

It worked for me... cheers

u01jmg3
  • 712
  • 1
  • 11
  • 31
utkarshk
  • 147
  • 7
3

There are things that still surprises me... I have a form with dynamic behaviour for two different entities. One entity requires some fields that the other don't. So, my JS code, depending on the entity, does something like: $('#periodo').removeAttr('required'); $("#periodo-container").hide();

and when the user selects the other entity: $("#periodo-container").show(); $('#periodo').prop('required', true);

But sometimes, when the form is submitted, the issue apppears: "An invalid form control with name=periodo'' is not focusable (i am using the same value for the id and name).

To fix this problem, you have to ensurance that the input where you are setting or removing 'required' is always visible.

So, what I did is:

$("#periodo-container").show(); //for making sure it is visible
$('#periodo').removeAttr('required'); 
$("#periodo-container").hide(); //then hide

Thats solved my problem... unbelievable.

Gustavo Soler
  • 1,469
  • 2
  • 9
  • 6
3

In my case..

ng-show was being used.
ng-if was put in its place and fixed my error.

3

Wow, a lot of answers here!

If the problem is <input type="hidden" required="true" />, then you can solve this in just a few lines.

The logic is simple and straight-forward:

  1. Mark every required input on page-load with a data-required class.
  2. On submit, do two things: a) Add required="true" to all data-required inputs. b) Remove required="true"` from all hidden inputs.

HTML

<input type="submit" id="submit-button">

Pure JavaScript

document.querySelector('input,textarea,select').filter('[required]').classList.add('data-required');

document.querySelector('#submit-button').addEventListener('click', function(event) {
    document.querySelector('.data-required').prop('required', true);
    document.querySelector('input,textarea,select').filter('[required]:hidden').prop('required', false);
    return true;
}

jQuery

$('input,textarea,select').filter('[required]').addClass('data-required');

$('#submit-button').on('click', function(event) {
    $('.data-required').prop('required', true);
    $('input,textarea,select').filter('[required]:hidden').prop('required', false);
    return true;
}
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
2

For Angular use:

ng-required="boolean"

This will only apply the html5 'required' attribute if the value is true.

<input ng-model="myCtrl.item" ng-required="myCtrl.items > 0" />
Nick Taras
  • 696
  • 8
  • 15
2

I found same problem when using Angular JS. It was caused from using required together with ng-hide. When I clicked on the submit button while this element was hidden then it occurred the error An invalid form control with name='' is not focusable. finally!

For example of using ng-hide together with required:

<input type="text" ng-hide="for some condition" required something >

I solved it by replacing the required with ng-pattern instead.

For example of solution:

<input type="text" ng-hide="for some condition" ng-pattern="some thing" >
KHACHORNCHIT
  • 2,222
  • 23
  • 19
2

Not just only when specify required, I also got this issue when using min and max e.g.

<input type="number" min="1900" max="2090" />

That field can be hidden and shown based on other radio value. So, for temporary solution, I removed the validation.

deerawan
  • 8,002
  • 5
  • 42
  • 51
2

I have seen this question asked often and have come across this 'error' myself. There have even been links to question whether this is an actual bug in Chrome.
This is the response that occurs when one or more form input type elements are hidden and these elements have a min/max limit (or some other validation limitation) imposed.

On creation of a form, there are no values attributed to the elements, later on the element values may be filled or remain unchanged. At the time of submit, the form is parsed and any hidden elements that are outside of these validation limits will throw this 'error' into the console and the submit will fail. Since you can't access these elements (because they are hidden) this is the only response that is valid.

This isn't an actual fault nor bug. It is an indication that there are element values about to be submitted that are outside of the limits stipulated by one or more elements.

To fix this, assign a valid default value to any elements that are hidden in the form at any time before the form is submitted, then these 'errors' will never occur. It is not a bug as such, it is just forcing you into better programming habits.

NB: If you wish to set these values to something outside the validation limits then use form.addEventListener('submit', myFunction) to intercept the 'submit' event and fill in these elements in "myFunction". It seems the validation checking is performed before "myFunction() is called.

Kent
  • 383
  • 3
  • 5
1

Its because there is a hidden input with required attribute in the form.

In my case, I had a select box and it is hidden by jquery tokenizer using inline style. If I dont select any token, browser throws the above error on form submission.

So, I fixed it using the below css technique :

  select.download_tag{
     display: block !important;//because otherwise, its throwing error An invalid form control with name='download_tag[0][]' is not focusable.
    //So, instead set opacity
    opacity: 0;
    height: 0px;

 }
sudip
  • 2,781
  • 1
  • 29
  • 41
1

For other AngularJS 1.x users out there, this error appeared because I was hiding a form control from displaying instead of removing it from the DOM entirely when I didn't need the control to be completed.

I fixed this by using ng-if instead of ng-show/ng-hide on the div containing the form control requiring validation.

Hope this helps you fellow edge case users.

J.D. Mallen
  • 4,339
  • 3
  • 22
  • 33
1

Let me put state my case since it was different from all the above solutions. I had an html tag that wasn't closed correctly. the element was not required, but it was embedded in a hidden div

the problem in my case was with the type="datetime-local", which was -for some reason- being validated at form submission.

i changed this

<input type="datetime-local" />

into that

<input type="text" />
SoliQuiD
  • 2,093
  • 1
  • 25
  • 29
1

I wanted to answer here because NONE of these answers, or any other Google result solved the issue for me.

For me it had nothing to do with fieldsets or hidden inputs.

I found that if I used max="5" (e.g.) it would produce this error. If I used maxlength="5" ... no error.

I was able to reproduce the error and clearing of error several times.

I still do not know why using that code produces the error, as far as this states it should be valid, even without a "min" I believe.

Kevin Marmet
  • 172
  • 2
  • 9
1

with custom elements using the ElementInternals API the element can't use delegatesFocus: true with the shadowRoot because the element has to receive focus directly (currently, this may evolve) within the exposed form's DOM tree (the parent form can be in a shadowRoot, it simply must be in the same portion of the tree with the custom and other form elements); the element must be visible and in my experiment tabindex=0 was required on the element (-1 also seems to work)

a demo expanded from googlechrome with a custom element is at https://jimmont.github.io/samples/report-validity/

the error is simply reporting that an element which has attributes indicating it needs validation cannot receive focus when there's a problem with the validation, so the user can correct the error; if the error and information is coming in as expected you can ignore the error; otherwise it's likely the result of some UI features hiding elements and a mix of unexpected behavior; it's possible to use another approach in implementations to allow the user to walk back through a flow of some sort, but it appears that's beyond the scope of the question at the moment

jimmont
  • 2,304
  • 1
  • 27
  • 29
1

For me the problem was a input type email inside div with visibility: hidden, something like this

<form>
  <div style="visibility: hidden;">
  <input type="email" name="email" id="email">
  ............
</form>

then You can change type="email" to type="text" or remove visibility: hidden;

luis urdaneta
  • 91
  • 1
  • 4
1

Inside your form input. You keep the required in Hidden Input that's why it throws:

An invalid form control with name='' is not focusable

Check your input name that throws invalid, which you keep hidden and required.

Just, remove the required from the hidden input name. Done.

Y. Joy Ch. Singha
  • 3,056
  • 24
  • 26
0

Alternatively another and foolproof answer is to use HTML5 Placeholder attribute then there will be no need to use any js validations.

<input id="elemID" name="elemName" placeholder="Some Placeholder Text" type="hidden" required="required">

Chrome will not be able to find any empty, hidden and required elements to focus on. Simple Solution.

Hope that helps. I am totally sorted with this solution.

utkarshk
  • 147
  • 7
0

I came here with the same problem. For me (injecting hidden elements as needed - i.e. education in a job app) had the required flags.

What I realized was that the validator was firing on the injected faster than the document was ready, thus it complained.

My original ng-required tag was using the visibility tag (vm.flags.hasHighSchool).

I resolved by creating a dedicated flag to set required ng-required="vm.flags.highSchoolRequired == true"

I added a 10ms callback on setting that flag and the problem was solved.

 vm.hasHighSchool = function (attended) {

        vm.flags.hasHighSchool = attended;
        applicationSvc.hasHighSchool(attended, vm.application);
        setTimeout(function () {
            vm.flags.highSchoolRequired = true;;
        }, 10);
    }

Html:

<input type="text" name="vm.application.highSchool.name" data-ng-model="vm.application.highSchool.name" class="form-control" placeholder="Name *" ng-required="vm.flags.highSchoolRequired == true" /></div>
James Fleming
  • 2,589
  • 2
  • 25
  • 41
  • Your solution in fact is very, very risky. Anyone with slow cpu will still get same error. It's (or will be) typical race condition. Probably old smartphones might be affected. So please be cautious with this solution. – Drachenfels Apr 21 '15 at 16:53
0

Make sure that all of the controls in your form with the required attribute also have the name attribute set

Adam Diament
  • 4,290
  • 3
  • 34
  • 55
0

This error happened to me because I was submitting a form with required fields that were not filled.

The error was produced because the browser was trying to focus on the required fields to warn the user the fields were required but those required fields were hidden in a display none div so the browser could not focus on them. I was submitting from a visible tab and the required fields were in an hidden tab, hence the error.

To fix, make sure you control the required fields are filled.

eloone
  • 4,248
  • 2
  • 32
  • 35
0

I received the same error when cloning an HTML element for use in a form.

(I have a partially complete form, which has a template injected into it, and then the template is modified)

The error was referencing the original field, and not the cloned version.

I couldn't find any methods that would force the form to re-evaluate itself (in the hope it would get rid of any references to the old fields that now don't exist) before running the validation.

In order to get around this issue, I removed the required attribute from the original element and dynamically added it to the cloned field before injecting it into the form. The form now validates the cloned and modified fields correctly.

AlastairDewar
  • 77
  • 1
  • 6
0

My scenario I hope not missed in this lengthy seed of answers was something really odd.

I have div elements that dynamically update through a dialogBox being called in them to load and get actioned in.

In short the div ids had

<div id="name${instance.username}"/>

I had a user: 测试帐户 and for some reason the encoding did some strange stuff in the java script world. I got this error message for a form working in other places.

Narrowed it down to this and a retest of using numeric numbers instead i.e id appears to fix the issue.

V H
  • 8,382
  • 2
  • 28
  • 48
0
<input type="submit" name="btnSave" value="Update" id="btnSave" formnovalidate="formnovalidate">

formnovalidate="formnovalidate"

E Demir
  • 81
  • 1
  • 2
0

This is happening to me because I am using the Materialize JS/CSS Framework. When I create a <select> field in my Materialized form, it is converted to a managed field with many stylized fields and the real <select> field is hidden by the framework, serving only to deliver the selected value to the post request.

So it will always show the console error unless I use the browser-default class to invalide Materialize management over my <select> field.

Other way around is to use a button with type="button" instead of type="submit", and use $('form').submit() to post send the form, but you will loose the browser validation UI and will have to develop your own, or use the framework's.

NaN
  • 8,596
  • 20
  • 79
  • 153
0

You can disable the HTML5 validation if you don't want it as mentioned in other replies. But if you still want the HTML5 validation, you can add a "click" event listener to the submit button to display the hidden fields. So the click event will make the fields visible and the validation won't throw that error anymore.

ezzadeen
  • 1,033
  • 10
  • 9
0

This might help someone, mine occurred because I had no closing tag for the form.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34001370) – ugexe Mar 16 '23 at 00:25