How not to validate HTML5 text box with required attribute - html

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.

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>

Try to add formnovalidate attribute to your cancel button
<button type="button" formnovalidate>Cancel</button>

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

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>

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

try this one.it works fine.It will work on submit button
<asp:TextBox runat="server" ID="inputusername" required />

Related

All buttons in <form> are posting

I have a form with multiple textboxes and buttons. I want to use some of the buttons to clear the textbox. But when I click on the button Clear text the form gets posted.
Does someone know how I can fix this?
Here is my HTML:
<form name="reaction" method="post" action="./send.php">
<input type="text" name="firstname">
<button class="btn btn-default" onclick="ClearText1();">Clear text</button><br />
<input type="text" name="lastname">
<button class="btn btn-default" onclick="ClearText2();">Clear text</button><br />
<button class="btn btn-block btn-success">Send</button>
</form>
use <button type="submit" for submit button and <button type="button" for other button.You can read More about button here.
From MDN
The type attribute of the button. Possible values are:
submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.
menu: The button opens a popup menu defined via its designated element.
<form name="reaction" method="post" action="./send.php">
<input type="text" name="firstname">
<button type="button" class="btn btn-default" onclick="ClearText1();">Clear text</button><br />
<input type="text" name="lastname">
<button type="button" class="btn btn-default" onclick="ClearText2();">Clear text</button><br />
<button type="submit" class="btn btn-block btn-success">Send</button>
</form>
Use event.preventDefault(); to prevent default submit behavior for this button.
<script>
function ClearText2(event){
// your code....
event.preventDefault();
}
</script>

Form submits when pressing ENTER

I'm on Home/Index. I have the following HTML:
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
For some reason, if I have the cursor in the ConfirmationCode input and I press the Enter key, the form submits, and redirects to http://localhost:62500/?ConfirmationCode= . The thing is, I've read about this behaviour and I understood it might be somewhat intended behaviour depending on browser and whatnot. But I have this other form,
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="button" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
which doesn't have this behaviour, and nothing happens when I press the Enter key.
The form submits because you hace only 1 input in your form (no additional data needs to be entered).
Change you first form to :
<form id="frmCode" style="display: inline-block">
<input type="text" name="ConfirmationCode"/>
<input type="text" name="ConfirmationCode2"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
and the form will not submit when you press Enter
If you want to disable the submition functionality of the form you can add the onsubmit event handler like this:
<form id="frmCode" style="display: inline-block" onsubmit="return false;">
<input type="text" name="ConfirmationCode"/>
<input type="button"/>
<img src="~/Images/loading.gif" id="notificationLoading"/>
</form>
You need to set the type of the button you want to use to submit your form to submit.
<input type="submit" ...>
<form id="frmLogin" class="form-horizontal" role="form">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-sm-8">
<input type="text" name="MailOrUsername" title="Te poți loga introducând mail-ul sau numele de utilizator" data-val="true" data-val-required="De ce apeși aiurea? Bagă ID." class="form-control" placeholder="Mail sau ID" />
</div>
<div class="col-sm-8">
<input type="password" name="Password" title="Introdu parola asociată contului tău" data-val="true" data-val-required="Bagă parola." class="form-control" placeholder="Parola" />
</div>
<input type="checkbox" name="RememberMe" />
<span title="Bifând căsuța asta rămâi autentificat și după ce închizi browserul">Ține-mă minte</span>
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
<input type="button" onclick="login_hide()" />
<img src="~/Images/loading.gif" id="loginLoading" />
</form>
More infos on the input element and on control types.
That is the usual browser behaviour, when a user presses enter when using a form.
The form will submit and as there is no action attribute in your form tag, it will go back to the current URL (with any of the form fields/values attached) ?ConfirmationCode=
As mentioned in 'Ubiquitous Developers' comment; your are using html buttons <input type="button"/> and not the html submit button <input type="submit" />. This will not have the default behaviours of the submit button.
Its probable that the writer of the script you had, didn't want to use the default behaviours of html forms. Instead using javascript to decide how the form will behave.
I think that they use some javascript to prevent the submission of the form like this;
How to prevent ENTER keypress to submit a web form?
The first form is a simple form which follows the default browser rules. The rule is pressing enter means clicking the submit button.
<input type="button"/>
above is not a valid submit button, so the browser submits the form.
In the second case the scenario is diffrerent.
<input type="submit" onclick="login()" id="btnLogin" style="margin-top: 7px; margin-bottom: -5px" value="Intră" class="btn btn-info" />
here this is a valid submit button and so in pressing enter key, the browser clicks the button and this button instantly calls the JavaScript function "login()". Now it must be in the code that it does something that prevents the form from submission. may be something like "return false;". So the form doe snot submit.
You can try this. it works
<form method="post" action="yourpage.html">
<input type="text" name="name" class="inputbox" />
<button type="submit" id="btn1">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script>
$(".inputbox").on('keyup', function (e) {
if (e.keyCode === 13) {
$("#btn1").click();
}
});
</script>

Multiple submit buttons - binding of text field to submit button

I have a simple HTML form with multiple submit buttons and I want my application to act differently on whichever submit button I click.
Example:
<form method="POST">
<input type="submit" value="test" name="test" />
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search" />
<input type="text" name="search_query2" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search2" />
<input type="submit" value="GO" name="next" />
</form>
This works for me, and I know how to distinguish which button I clicked. The problem I have is that if I push Enter while editing the text-field then the first submit button gets clicked (the one named test). Is this possible to let the browser know (with HTML only - the whole point of this is to make it work with NO JavaScript) that I pushed Enter and it should send search with POST, not test?
So is there some kind of binding of text field to the submit button?
A solution to this problem would be to have multiple form elements, including your different submit buttons and the corresponding textfields.
I can't think of a solution with just one formular but no Javascript in use.
You could move the one you want to be submitted to first in that list, like so:
<form method="POST">
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some text">
<input type="submit" value="Search" name="search" />
<input type="submit" value="test" name="test" />
<input type="submit" value="GO" name="next" />
</form>
But it seems that your HTML is just poorly formed, I'd recommend Merguez's answer
Since your edit, there is no way to do this without Javascript/jQuery.
This would not be hard to do with PHP.
First: Change your buttons to actual controls:
becomes
<form method="POST">
<input type="text" name="search_query" style="width: 300px; padding: 5px" value="some
text" />
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Search">Search</button>
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Test">Test</button>
<button type="submit" name="actionB" onclick="javascript:checkBtn();" value="Go">Go</button>
</form>
Now, when the form is submitted, check the returned value of actionB. If it's "Search", then piece together your search URL using the returned value of search_query.
There is NO way to read variables using only HTML. JavaScript is required if you're not going to use a scripting language to do this. You can totally do this in native JavaScript. JQuery is NOT required for this relatively simple function.
<script language="JavaScript">
function checkBtn() {
var btnX=document.getElementById("actionB").value;
if (btnX=="Search") {
// compose your new URL here
}
// repeat for each of the possible buttons
</script>
Hope that helps somewhat!
Steve

custom html button code for a form

ttI know this is a lame question but in order to do it right i need your help.
this is my html code
<form name="form1" class="form1">
<fieldset>
<legend>Subscription</legend>
<label for="subname">Name</label>
<input type="text" name="subname" />
<label style="padding-left:20px;" for="subemail">Your Email</label>
<input type="text" name="subemail" />
</fieldset>
</form>
<div style="width:100%; font-size:14px;text-align:center;">Click here to subscribe
</div>
<div class="subscribe">
</div>
and here is the fiddle
http://jsfiddle.net/V8RB2/
how can i associate the "Click here to subscribe" text and the button with this form?
If i understood you correctly, you want to submit the form when the link is clicked. To do this, you'll have to use JavaScript.
A quick way of doing it is by telling the form to submit itself in the onclick attribute of the link.
<a onclick="form1.submit()" href="#">Click here to subscribe</a>
What happens is, that when the link is clicked, you trap its onclick event and then you tell form1 to submit itself.
The easiest way would make this a button within the form and use CSS to style it however you want.
<input type="submit" id="button1" class="button1" name="subscribe" value="Click here to subscribe!" />
Then to style:
form input.button1 {
style here
}
The below will work.
<form name="form1" class="form1">
<fieldset>
<legend>Subscription</legend>
<label for="subname">Name</label>
<input type="text" name="subname" />
<label style="padding-left:20px;" for="subemail">Your Email</label>
<input type="text" name="subemail" />
</fieldset>
Click here to subscribe
</form>
Change your link's href to:
href="javascript:document.forms['form1'].submit()"
Or add that as an onClick attribute.
Updating with a technique to route around disabled javascript.
<script>document.write('Click here to subscribe</div><div class="subscribe">');</script><noscript><input type="submit" value="submit"></noscript>

Buttons go on the same address

I have the following html code
gogu#yahoo.com wants to be your partner. Do you agree?
<br><input type="button" id="1" name="btn1" class="button" value="Yes" onclick="window.location='confirm/gogu#yahoo.com';" />
<input type="button" id="2" name="btn2" class="button" value="No" onclick="window.location='decline/gogu#yahoo.com';" />
The problem is when i click on the No button it gets me on the same address confirm/gogu#yahoo.com.
How can i redirect properly?
Try using window.location.href=