Disabling native form validation for a certain button - html

I have an HTML form in which I use the browser's native validation. However, inside my form I have another button unrelated to the submit that does a different action. When I press that button howver, it fires off the browser's validation, how can I disable that
<form>
<input type="text" required />
<button>I shouldn't fire validation</button>
<input type="submit" value="I fire the validation">
</form>

In HTML 5, button has a default behavior as a submit type. So
<button type="button">Button</button>

A button by it's own don't fire the submit. You probably have an event setted on it. If you don't, try this other sol
You could use
<form>
<input type="text" required />
<button type="button">I shouldn't fire validation</button>
<input type="submit" value="I fire the validation">
</form>
It seems weird, but I got if from this example (just in the end of 4.9 section):
http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#concept-button
And you can see it working: http://jsfiddle.net/xmhpF/

Related

Button in Angular form being "clicked" when I press enter

I'm have a strange issue in my Angular form. I have a simple form like the following:
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<button (click)="addUser()">Add a user</button>
<input name="first" ngModel required #first="ngModel">
<input name="last" ngModel>
<button>Submit</button>
</form>
The problem is that when I press ENTER when in the form, instead of submitting, it fires the function addUser(), which is not what I want it to do.
How do I stop it from "clicking" ADD A USER and firing addUser()?
The solution was simply to add the type attribute with value button to the first button.
The browser assigns type="submit" by default to all buttons.
Simply needed to change
<button (click)="addUser()">Add a user</button>
to
<button (click)="addUser()" type="button">Add a user</button>

Enter key action when multiple submit buttons exist on a single form

I'm running into a strange issue where Internet Explorer is adding an additional query string parameter that no other browser adds.
The page has a form with auto-submit functionality and a "Reset Filters" button. When a user hits the enter key, it forces the submit. When a user hits enter in Internet Explorer, for some reason the "Reset Filters" operation is selected rather than the submit button.
For example, IE adds this to the query string:
?search=this+is+text&op=Reset+Filters
In all other browsers the same query looks like this:
?search=this+is+text
When I check the $_GET superglobal in PHP, I noticed that op is only being added when I run the application in IE and only when I hit the enter key in the form.
Based on the HTML below, it kind of makes sense that hitting enter would add op to the query string because both the submit button and the reset button are contained within the form. But why would op only get added to IE?
<form>
...
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
<div class="reset-button">
<input type="submit" id="edit-reset" name="op" value="Reset Filters" class="form-submit">
</div>
...
</form>
Any idea why this might be happening?
UPDATE: One other piece of information that might be important. Because the form is auto-submit, the first submit button is actually hidden. I'm wondering if that's why IE is using the second button as the submit handler.
After doing some more research I realized I asked the wrong question. However, it's not letting me delete the question, so I'm posting the answer to my actual question here.
My question should have been, "When multiple inputs exist in a single form, how does the browser determine which one is chosen when hitting the enter key?"
The answer is, when the enter key is hit, the first input of type="submit" is chosen. However, IE will skip any submit buttons that are hidden with display:none.
I found the answer here:
Multiple submit buttons on HTML form – designate one button as default
My fix was to set the submit button to position: absolute; left: -1000% rather than display:none. I got that solution from #bobince on the linked answer, however, left:-100% did not push it completely off the page for me so I changed it to left:-1000%.
IMHO it seems wrong to be using a submit button do convey some information other than "hey, I've submitted some data". If the user hits enter to submit the form it is reasonable that some browsers would send all the data associated with all the submit buttons.
If you are just resetting the inputs from previous parts of the form you could use:
<button type="reset">
If you do need other input data maybe a checkbox would be more appropriate:
<form>
...
<input type="checkbox" id="edit-reset" name="op" value="Reset Filters">
<label for="edit-reset">Reset Filters</label>
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
...
</form>
If you do not need other input data you could use two forms:
<form>
...
<div class="submit-button">
<input class="form-submit" type="submit" id="edit-submit-fda-views" name="" value="Submit">
</div>
</form>
<form>
<div class="reset-button">
<input type="submit" id="edit-reset" name="op" value="Reset Filters" class="form-submit">
</div>
</form>
A submit button is an input. It has a name and a value. When you click on one of the submit buttons, it's value gets added to the the submission with it's name. When you hit the enter key, the form is automatically submitted, but since you are using two submit buttons, they are both contributing a parameter. You have a lot of options that others have already suggested. You could change the type to "reset" or "button", but if you need to post to the server for both actions, then you could intercept the keystroke with javascript and click the button in code. I would probably go with a button type that would submit the form like this.
<input type="button" id="edit-reset" name="op" value="Reset Filters"
class="form-submit" onclick="submitform()">
<Script>
function submitform()
{
document.getElementById("your-form-name-here").submit();
}
</script>

What happens if we use mulptile button tags HTML with type as submit

I'm developing MVC application where I want to use button tags as it is the best tag as far as I know.
Can I use multiple HTML Button tags with <button type="submit">...</button>.
If you use more than one type=submit, all send the same parent form, but you can put differents submit buttons in differentes forms in the same page.
Both buttons will call submit action, example:
<form>
<input required type="text">
<button type="submit">submit</button>
<button type="submit">submit</button>
</form>
If u will not use any type, submit action will be called to, example:
<form>
<input required type="text">
<button>submit</button>
<button>submit</button>
</form>
If button will have type 'button' then submit action will not be called, example:
<form>
<input required type="text">
<button type="submit">submit</button>
<button>submit</button>
<button type="button">not</button>
</form>
You can use multiple button tags with type="submit" in a single form tag, only if you want that form is submitted after clicking on that button. And you can differ between those buttons by giving different names to them.

HTML form press 'Enter' in an input text cause click on button

http://jsfiddle.net/PCGvG/
Why in this example when I press 'Enter' in the input text, it executes the onclick? While using <input type="button"> doesn't...
The element <button> has type="submit" by default. See http://www.w3.org/TR/html401/interact/forms.html#h-17.5
Solution: if you don't want the click handler of the button to be called, give it type="button" explicitly.
<form name="myForm">
<button type="button" id="test">Click</button>
<input type="text" name="q" />
</form>
(Pressing Enter will still submit the form though, at least on some browsers.)
With regards to your comment that <input type="button"> acts differently: that's right. An input with type="button" acts the same way as a button with type="button", while an input with type="submit" would act the same as a button with type="submit".

Chrome doesn't disable submit button with span

If I have a submit button with a span inside it, it looks like Chrome still allows the span to be clicked when the button is set to disabled.
<form method="post" action="/changestatus" id="yw0">
<input type="hidden" name="id" value="5">
<input type="hidden" name="status" value="enabled">
<button type="submit" disabled="disabled">
<span>Submit</span>
</button>
</form>
The preceding code works as expected in Firefox, but not in Chrome.
Any ideas on fixing this (without JS if possible).
Thanks!
For submitting the form it does not submit in either browser (as it should).
If, though, with allows the span to be clicked you mean javascript firing the click event when clicking on it, then indeed there is a difference on how they handle this case.. (you will have to handle it through javascript)
If you don't want the form to submit you can add this code to your form element.
<form onSubmit="return false;">
I know its a JavaScript fix, but it is one.