How not to validate HTML5 text box with required attribute
Asked Answered
I

6

21

I have one HTML text box (for quantity) and two HTML buttons (Add, Cancel) inside my form.

<form>
    <input type="number" min="1" max="100" required />
    <button type="button">Add</button>
    <button type="button">Cancel</button>
</form>

I do not want my second button (cancel) to validate the form when clicked.

Is this possible? In ASP.NET, I can use CausesValidation="false" to not trigger the validation.

Ibis answered 13/8, 2013 at 7:10 Comment(1)
Reading material: #11761702Lag
S
44

Try this;

<button type="button" formnovalidate>Cancel</button>

I changed your code: What you want is that your form should not be validated on click of cancel, so that's why i added the formnovalidate to the button cancel.

<form>
    <input type="number" min="1" max="100" required />
    <input type="submit">Add</input>
    <input type="submit" formnovalidate>Cancel</input>
</form>
Sawicki answered 13/8, 2013 at 7:12 Comment(3)
Which one is appropriate to use? formvalidate or novalidate?Ibis
Mix of your 2 answers... formnovalidate :)Visage
Not working for ASP.NET ImageButton even though I add CausesValidation="false" and formnovalidate for IE11Sekyere
V
4

Try to add formnovalidate attribute to your cancel button

<button type="button" formnovalidate>Cancel</button>
Visage answered 13/8, 2013 at 7:13 Comment(0)
M
3

You could use the formnovalidate-option on the cancel-button, like this:

<input name="cancel" type="submit" value="Cancel" formnovalidate/>

See Symfony2 form validation with html5 and CANCEL button

Mcginnis answered 13/8, 2013 at 7:19 Comment(0)
S
2

If you have the input button as runat=server then you must have the property causesvalidation="false" or else if you are simply using html input type button for cancel use javascript to redirect to your page on click of cancel.

For eg. 1)

<input type="button" id="btnCancel" class="button blue" value="Cancel" **causesvalidation="false"** onserverclick="btnCancel_Click" runat="server" />

2)

<input type="button" id="btnCancel" class="button blue" value="Cancel" **onclick="GoTo();"**/>

<script type="text/javascript">
function GoTo() {
            window.location.href = '../default.aspx';
        }
</script>
Sericeous answered 9/10, 2013 at 5:58 Comment(0)
V
1

Try this code at asp.net 5enter link description here button control

<form>
<input type="number" min="1" max="100" required />
<button type="button">Add</button>
<button type="button" formnovalidate="formnovalidate">Cancel</button>

Aldo Flores Reyes @alduar .net news

Vanhouten answered 19/8, 2014 at 14:32 Comment(0)
G
0

try this one.it works fine.It will work on submit button

<asp:TextBox runat="server" ID="inputusername" required />
Golgi answered 26/4, 2017 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.