jQuery validation plugin disabling button [duplicate] - javascript

I am trying to stop a submit button to be clicked multiple times and upload the same item multiple times to MySQL.
Because the validation doesn't work, I can not press the button submit button at all. Which means I can't test the code.
I am running related post as well here, to give you some more information.
What I've got so far, using jQuery validation:
$("#UploadForm").validate({
errorLabelContainer: "#messageBox",
wrapper: "td",
rules: {
auction_description: {
required: true
},
auction_int_postage_type: {
required: true
},
listing_type: {
required: true
}
}
//all your options here
submitHandler:function(form){
$('#submit').attr('disabled','disabled');
}
});
HTML
<form method="post" name="UploadForm" id="UploadForm"
action="upload.php" enctype="multipart/form-data" >
<input style="text-transform:none;" type="text" class="button_date"
name="auction_bin_price" id="auction_bin_price" value="" size="15" />
<input class="button2" style="border-right:none; font-size:13px;"
name="List Item" id="submit" type="submit" value="List Item"/>
</form>

Here is a fiddle that has a working demo of solution to your problem.
jQuery 1.6+
To change the disabled property you should use the .prop() function.
$("#submit").prop('disabled', true);
$("#submit").prop('disabled', false);
jQuery 1.5 and below
The .prop() function doesn't exist, but .attr() does similar:
Set the disabled attribute.
$("#submit").attr('disabled','disabled');
To enable again
$("#submit").removeAttr('disabled');
In any version of jQuery
You can always rely on the actual DOM object and is probably a little faster than the other two options if you are only dealing with one element:
// assuming an event handler thus 'this'
this.disabled = true;
The advantage to using the .prop() or .attr() methods is that you can set the property for a bunch of selected items.

Related

jquery validation ignore and include [duplicate]

This question already has answers here:
jQuery Validation multiple selectors for ignore
(4 answers)
Closed 5 years ago.
I have a form with large set of fields that includes both read only and hidden fields. I want to enable validation for hidden field but disable validation for ready only fields
In the new version of jQuery validation plugin by the default validation of hidden fields are ignored. So to enable it I have to use :
$("form[name='Formname']").validate({
ignore: []
});
and it is working fine. But I need to ignore the validation for readonly fields and for that I need to use
$("form[name='Formname']").validate({
ignore: [readonly=readonly]
});
Even if i merge the two,it still doesn't work because than it only ignore readonly but doesn't apply vaildation on hidden field
You have to pass selector to ignore like this
ignore: '[readonly]', // This will select all input with readonly and ignore
Elements to ignore when validating, simply filtering them out.
jQuery's not-method is used, therefore everything that is accepted by
not() can be passed as this option. Inputs of type submit and reset
are always ignored, so are disabled elements.
SNIPPET
$("form[name='Formname']").validate({
ignore: '[readonly]',
rules: {
field: {
required: true
},
field1: {
required: true
}}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<form name="Formname">
<input readonly name="field1">
<input type="text" id="field" name="field">
<input type="submit" value="Validate!">
</form>

Check if checkbox is checked then send

I have this markup:
<form action="http://acumbamail.com/signup/13565/" method="POST">
<input type="checkbox" id="privacidad-btn" > Acepto polĂ­tica de privacidad<br>
<input type="button" value="Enviar" id="submit_acumba">
</form>
I want that if the user clicks on the button without checkbox checked there is an alert that he must agree to the terms (check the checkbox). Any ideas on the best approach to this?
I'm starting doing this way but don't know how which way to go:
if (jQuery("#privacidad-btn").is(":checked")) {
}
One approach that i like with html5 is the form validation
just put required on the checkbox and when the try to submit it they will be alerted with a popover dialog in there own language (its a good highlighter in the form of what is wrong with it)
<input required type="checkbox" id="privacidad-btn">
You could do it the way tymeJV suggest with button clicked event $("#submit_acumba").click(...)
That way you would support more browsers. but: It would just only validate on a click of a button
But there is the form submit event as well.
$("form").submit(function(e) {
if ( ! jQuery("#privacidad-btn").is(":checked")) {
// Not checked abort the default submit
e.preventDefault();
}
});
The difference is that it has to do all the native form validation before -> if it is invalid it won't trigger a submit or call the function
with button.onclick it would avoid the native validation since it would run before the submit event
You need a handler for the button as well:
$("#submit_acumba").click(function(e) {
e.preventDefault();
if (jQuery("#privacidad-btn").is(":checked")) {
//submit!
}
})
Using this straight and simple HTML implementation, you can do this without any special scripting (JavaScript/jQuery):
<form>
<p><input type="checkbox" required name="terms"> I accept the <u>Terms and Conditions</u></p>
<p><input type="submit"></p>
</form>
Here's a JSFiddle link where you can play with this implementation: http://jsfiddle.net/zs9b167b/

Jquery form validator tooltip styling disappears

Using jQuery Form Validation plugin, I'm able to get a styled tooltip (JSFiddle) to appear without any CSS. However, when I input this exact script into my page, it disappears.
JS:
$(function () {
$('#ajaxform').validate({ // initialize the plugin
// e.preventDefault();
submitHandler: function (form) {
//ajax do stuff
setTimeout(function () {
$('#messageResponse').fadeOut(500)
}, 5000);
// resets fields
$('#ajaxform')[0].reset();
//alert('form submitted via ajax');
return false; // blocks redirect after submission via ajax
}
});
});
Form:
<form name="ajaxform" id="ajaxform" method="post" action="ajax.php">
<input name="type" type="radio" value="1" required="">1</input>
<input type="radio" name="type" value="2">2</input>
<input name="type" type="radio" value="3">3</input>
<input name="submit" type="submit" id="submit" value="Submit"></input>
</form>
While playing with this script, I can get the tooltip to appear by uncommenting the e.preventDefault(); line.
Otherwise, it provides the unstyled error next to the input box. I can't figure out if I have unintended CSS applied to it, because Firebug won't select the popup. Can someone explain what is causing this tip to unstyle? Alternatively, can someone explain how to create the default tip that's on the fiddle?
You are using "required" attribute in your code which works with HTML5 based browsers. It has nothing to do with the plugin you are using. Check for the DOC Type in your page and change it to to make it work.
Though if you are planning to support it cross browser, you will have to write your own code to show the tooltip.

Disable Jquery UI Button on form submit

I have a form like so:
<form action="" method="post" id="form" name="form" enctype="multipart/form-data">
<input name="action" type="submit" value="Send" />
</form>
I am using the latest version of the Jquery UI Button plugin. The value of the submit button MUST be submitted. Is there an easy way to disable the submit button after the form has been submitted, without using additional plugins such as "Lock Submit" - http://plugins.jquery.com/project/LockSubmit ?
Many thanks.
When you set the disabled property of an element, it won't be submitted. The workaround is to set the disabled property after the form is submitted. You can use the setTimeout() function with a very short delay:
$("#form").submit(function() {
setTimeout(function() {
// .prop() requires jQuery 1.6 or higher
$("#form :submit").prop("disabled", true);
}, 100);
});
Demo here
Another trick, is to use a trick :) You can wrap the original submit button inside a div and hide it when the form submits.
Demo here
Easy just add type="button" to button attributes
like
<button type="button" >not submit</button>
you mean
$('#form').submit(function() {$(this).find('input:[type="submit"]').button({disabled:true})});
?
$('#form').submit(function() {
$(this).find('input[type="submit"]').disable();
});

How to submit a form when the return key is pressed?

Can someone please tell me how to submit an HTML form when the return key is pressed and if there are no buttons in the form?
The submit button is not there. I am using a custom div instead of that.
To submit the form when the enter key is pressed create a javascript function along these lines.
function checkSubmit(e) {
if(e && e.keyCode == 13) {
document.forms[0].submit();
}
}
Then add the event to whatever scope you need eg on the div tag:
<div onKeyPress="return checkSubmit(event)"/>
This is also the default behaviour of Internet Explorer 7 anyway though (probably earlier versions as well).
IMO, this is the cleanest answer:
<form action="" method="get">
Name: <input type="text" name="name"/><br/>
Pwd: <input type="password" name="password"/><br/>
<div class="yourCustomDiv"/>
<input type="submit" style="display:none"/>
</form>
Better yet, if you are using javascript to submit the form using the custom div, you should also use javascript to create it, and to set the display:none style on the button. This way users with javascript disabled will still see the submit button and can click on it.
It has been noted that display:none will cause IE to ignore the input. I created a new JSFiddle example that starts as a standard form, and uses progressive enhancement to hide the submit and create the new div. I did use the CSS styling from StriplingWarrior.
I tried various javascript/jQuery-based strategies, but I kept having issues. The latest issue to arise involved accidental submission when the user uses the enter key to select from the browser's built-in auto-complete list. I finally switched to this strategy, which seems to work on all the browsers my company supports:
<div class="hidden-submit"><input type="submit" tabindex="-1"/></div>
.hidden-submit {
border: 0 none;
height: 0;
width: 0;
padding: 0;
margin: 0;
overflow: hidden;
}
This is similar to the currently-accepted answer by Chris Marasti-Georg, but by avoiding display: none, it appears to work correctly on all browsers.
Update
I edited the code above to include a negative tabindex so it doesn't capture the tab key. While this technically won't validate in HTML 4, the HTML5 spec includes language to make it work the way most browsers were already implementing it anyway.
Use the <button> tag. From the W3C standard:
Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content.
Basically there is another tag, <button>, which requires no javascript, that also can submit a form. It can be styled much in the way of a <div> tag (including <img /> inside the button tag). The buttons from the <input /> tag are not nearly as flexible.
<button type="submit">
<img src="my-icon.png" />
Clicking will submit the form
</button>
There are three types to set on the <button>; they map to the <input> button types.
<button type="submit">Will submit the form</button>
<button type="reset">Will reset the form</button>
<button type="button">Will do nothing; add javascript onclick hooks</button>
Standards
W3C wiki about <button>
HTML5 <button>
HTML4 <button>
I use <button> tags with css-sprites and a bit of css styling to get colorful and functional form buttons. Note that it's possible to write css for, for example, <a class="button"> links share to styling with the <button> element.
Here is how I do it with jQuery
j(".textBoxClass").keypress(function(e)
{
// if the key pressed is the enter key
if (e.which == 13)
{
// do work
}
});
Other javascript wouldnt be too different. the catch is checking for keypress argument of "13", which is the enter key
I believe this is what you want.
//<![CDATA[
//Send form if they hit enter.
document.onkeypress = enter;
function enter(e) {
if (e.which == 13) { sendform(); }
}
//Form to send
function sendform() {
document.forms[0].submit();
}
//]]>
Every time a key is pressed, function enter() will be called. If the key pressed matches the enter key (13), then sendform() will be called and the first encountered form will be sent. This is only for Firefox and other standards compliant browsers.
If you find this code useful, please be sure to vote me up!
Use the following script.
<SCRIPT TYPE="text/javascript">
<!--
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
{
myfield.form.submit();
return false;
}
else
return true;
}
//-->
</SCRIPT>
For each field that should submit the form when the user hits enter, call the submitenter function as follows.
<FORM ACTION="../cgi-bin/formaction.pl">
name: <INPUT NAME=realname SIZE=15><BR>
password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
onKeyPress="return submitenter(this,event)"><BR>
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>
I use this method:
<form name='test' method=post action='sendme.php'>
<input type=text name='test1'>
<input type=button value='send' onClick='document.test.submit()'>
<input type=image src='spacer.gif'> <!-- <<<< this is the secret! -->
</form>
Basically, I just add an invisible input of type image (where "spacer.gif" is a 1x1 transparent gif).
In this way, I can submit this form either with the 'send' button or simply by pressing enter on the keyboard.
This is the trick!
Why don't you just apply the div submit styles to a submit button? I'm sure there's a javascript for this but that would be easier.
If you are using asp.net you can use the defaultButton attribute on the form.
I think you should actually have a submit button or a submit image... Do you have a specific reason for using a "submit div"? If you just want custom styles I recommend <input type="image".... http://webdesign.about.com/cs/forms/a/aaformsubmit_2.htm
Extending on the answers, this is what worked for me, maybe someone will find it useful.
Html
<form method="post" action="/url" id="editMeta">
<textarea class="form-control" onkeypress="submitOnEnter(event)"></textarea>
</form>
Js
function submitOnEnter(e) {
if (e.which == 13) {
document.getElementById("editMeta").submit()
}
}
Similar to Chris Marasti-Georg's example, instead using inline javascript.
Essentially add onkeypress to the fields you want the enter key to work with. This example acts on the password field.
<html>
<head><title>title</title></head>
<body>
<form action="" method="get">
Name: <input type="text" name="name"/><br/>
Pwd: <input type="password" name="password" onkeypress="if(event.keyCode==13) {javascript:form.submit();}" /><br/>
<input type="submit" onClick="javascript:form.submit();"/>
</form>
</body>
</html>
Since display: none buttons and inputs won't work in Safari and IE, I found that the easiest way, requiring no extra javascript hacks, is to simply add an absolutely positioned <button /> to the form and place it far off screen.
<form action="" method="get">
<input type="text" name="name" />
<input type="password" name="password" />
<div class="yourCustomDiv"/>
<button style="position:absolute;left:-10000px;right:9990px"/>
</form>
This works in the current version of all major browsers as of September 2016.
Obviously its reccomended (and more semantically correct) to just style the <button/> as desired.
Using the "autofocus" attribute works to give input focus to the button by default. In fact clicking on any control within the form also gives focus to the form, a requirement for the form to react to the RETURN. So, the "autofocus" does that for you in case the user never clicked on any other control within the form.
So, the "autofocus" makes the crucial difference if the user never clicked on any of the form controls before hitting RETURN.
But even then, there are still 2 conditions to be met for this to work without JS:
a) you have to specify a page to go to (if left empty it wont work). In my example it is hello.php
b) the control has to be visible. You could conceivably move it off the page to hide, but you cannot use display:none or visibility:hidden.
What I did, was to use inline style to just move it off the page to the left by 200px. I made the height 0px so that it does not take up space. Because otherwise it can still disrupt other controls above and below. Or you could float the element too.
<form action="hello.php" method="get">
Name: <input type="text" name="name"/><br/>
Pwd: <input type="password" name="password"/><br/>
<div class="yourCustomDiv"/>
<input autofocus type="submit" style="position:relative; left:-200px; height:0px;" />
</form>

Categories

Resources