I have used Google ReCaptcha at the bottom of my sign up form and used JavaScript to validate it so that if the ReCaptcha isn't completed, then the form won't submit and an alert will come on screen.
However, I have 3 other submit buttons that I use as navigation at the top of my page and if you click one of these at any point, the alert message to complete the captcha appears.
My JavaScript looks like this:
JavaScript
$('form').on('submit', function(e) {
if(grecaptcha.getResponse() == "") {
e.preventDefault();
alert("You Must Complete The CAPTCHA Form!");
} else {
alert("Complete!");
}
});
The navigation-like buttons I use:
Buttons
<form action="#">
<input type="submit" id="buttons" value="Enter"/>
</form>
Submission submit button at the bottom of the form:
Submit
<form action="?" method="POST">
<div id="html_element"></div>
<br>
<input type="submit" value="Submit">
</form>
I have tried adding:
id="final-submit">
to the final button and then changing .on('submit') to .on('#final-submit')
But then the alert doesn't appear at all.
Am I doing this ID technique wrong or would this simply not work?
Cheers
Edit
I don't want to change my <input type="submit"> into <a href="#" id="button">
You can add id's on the forms to differentiate one forms submission from other. Say the id of the form with recaptcha is "formid". Then use the following and all should be fine
$('#formid').on('submit', function(e) {
if(grecaptcha.getResponse() == "") {
e.preventDefault();
alert("You Must Complete The CAPTCHA Form!");
} else {
alert("Complete!");
}
});
Related
I'm creating a login form and in that if any input is wrong then i want to stay on the same page and notify the client to enter the correct input
I want to stay on this page http://localhost:8080/loginpage/ but after every click i'm redirected to http://localhost:8080/loginpage/?UserName=UserName&pword=&ConfirmPassword=&Email=Email&FirstName=First+Name&LastName=Last+Name&cars=male&Signup=Signup .
I have written a code for this but it does not seem to work.
if(t!==0)
{
var er="Email-id already exists";
window.location.reload(false);
document.getElementById("nemail").value=er;
document.getElementById("username").value=username;
document.getElementById("pword").value="";
document.getElementById("confpwd").value="";
document.getElementById("fname").value=fname;
document.getElementById("lname").value=lname;
document.getElementById("gender").value=gender;
}
I have tried to use several other methods like
window.location.replace('/loginpage/');
window.location.href="/loginpage/index.html";
But none of them works.
Please help!
There are two ways to prevent a form to submit.
Set form onsubmit="return false".
Register a submit event on a from. In the callback call event.preventDefault();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>1 way to prevent form to submit via onclick attribute</h2>
<form action="#submit-from-1-way" onsubmit="return validate(this)"><!--set onsubmit="return false"-->
<input name="value"/>
<button type="submit">Submit</button>
</form>
<h2>2 way to prevent form to submit via submit event</h2>
<form id="form" action="#submit-from-2-way"><!--set onsubmit="return false"-->
<input name="value"/>
<button type="submit">Submit</button>
</form>
<script>
console.log(location.href+'#'+location.hash+'?'+location.search);
function validate(form) {
return $(form).find('input').val();
}
$('#form').submit(function (e) {
if (!validate(this)) e.preventDefault();
});
</script>
You can use preventDefault() on the click event for a given html object. This will prevent the browser from taking the default action on a specific HTML object.
For example to prevent actoin on a link:
<body>
Click Me
<script>
function dontGo(event) {
event.preventDefault();
}
document.getElementById("clickMe").addEventListener("click",dontGo);
</script>
</body>
I have this normal form:
<form class="vform" action='http://example.com/someaction.php' method='post' id='myid' >
<input type="email" name="email" id="email" class="required email " placeholder="Enter Your Email" >
<input id="#before" type='submit' value="Submit">
</form>
I am using the jquery.validate.js plugin to validate the form, which is working fine. (Link)
WHAT IS REQUIRED:
Before the user is redirected upon successful validation of the form, there is this 'pause' (while its redirecting)... during this time, users are repeatedly hitting the submit button.
How can I hide/replace (maybe with a loading gif or something) the submit button IF the form is validated.
Meaning, disable or replace the input button with something else IF the form is validated.
THE VALIDATION CODE:
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery.validation/1.14.0/jquery.validate.js"></script>
<script>
$(document).ready(function() {
$(".vform").validate();
});
</script>
I tried adding some replacement/disable code after the validate function like this:
$(this).children('input[type=submit]').attr('disabled', 'disabled');
or
$('#before').replaceWith($('#after'));
But confused as to how to actually go about it.
Can anyone offer some insight?
EDIT: The input button shouldn't be replaced/disabled if the form validation is false
You can submitHandler of jQuery validate.
<script>
$(document).ready(function() {
$(".vform").validate({
submitHandler: function(form) { // <- pass 'form' argument in
$("#before").attr("disabled", true);
form.submit(); // <- use 'form' argument here.
}
});
});
</script>
In PHP program, I have JS function which validates submit <form ...onsubmit='return isOK();'>. It works OK. The problem is I want it to work only for particular submits, not for all. Is there any way inside the JS function to find out which submit was pressed, or some PHP trick
instead of onsubmit u can use onClick.
<input type="submit" onclick="return pressSubmit1()" value="submit1" />
<input type="submit" onclick="return pressSubmit2()" value="submit2" />
<form action="action.php" method="post">
...
<input name="submit" type="button" value="check me" onclick="submitform('check')" />
<input name="submit" type="button" value="do not check me" onclick="submitform('not check')"/>
</form>
in javascript:
function submitform(check)
{
if(check=='check') checkfrom();
}
in PHP
if($_POST['submit']=="check me")
checkform();
<form class="allowed" onsubmit='return isOK();'>
----
function isOK() {
if($(this).hasClass('allowed')) {
// Do stuff
}
}
your question is not clear, are you using a single submit button or different ones. If you're using different submit buttons then ofcourse you can make checks based on the id of the submit button. On the other hand if is a single submit button then it depends on what the conditions are for submitting or not submitting
It's better you write some HTML and Java Script code which you are using. So, it's easy to correct mistake is you have made somewhere in code snippet.
We can check which submit button is clicked, using PHP.
In the PHP page corresponding to the form submit, write
if(extract($_POST) && isset($submitbtn1)) {
// some validation for first submit button
}
elseif(extract($_POST) && isset($submitbtn2)) {
// some validation for second submit button
}
Note: we can use $ followed by submit button name inside the extract function. ie. $submitbtn1 is same as $_POST['submitbtn1']
Yes different onclick looks fine as mentioned in the accepted answer. But in the event handler you should have the
code as below. Note the preventDefault.
Also since submit is A button that submits the form. You can use button type instead of submit type and then there is no need for preventDefault. Here is the link of the code http://jsbin.com/wikose/4/
function testlogin(){
event.preventDefault();
var test = 'not validated';
if(test === "validated")
alert("not valid login");
else
$('form').submit();
return false;
}
What I want is to be able to submit a form when ever a user presses some key like tab or enter (i.e. that will case him to lose focus) also clicking outside of the text field should trigger a submit. However when ever he click on the cancel button it should prevent the submit ion
html structure look like this
<form id="form">
<input id="text" onblur="$(this).closest('form').submit();">
<a id"submit">submit</a>
<a id"cancel">cancel</a>
</form>
Currently what happens is that when a user presses enter a form is submitted twice and it should be submitted only once. When he presses a cancel a form is submitted and cancelled right after that.
Does anyone have any idea how can I write some javascript code that can accomplish this behaviour (the idea for is take form the jira in-line edit mode and I am trying to construct something in similar manner)
I'm going to provide you with another you could approach this by using jQuery and it's .change() event handler. This way when the user clicks off of the input element, it'll trigger the .change() event and you can submit a form within it's callback function. If the user cancels then you can handle that event normally, same with the submit button click.
The Form:
<form id="form">
<input id="text">
<a id="cancel">cancel</a>
<a id="submit">submit</a>
</form>
The Javascript (jQuery):
$(document).ready(function(){
$('#submit').click(function(e){
e.preventDefault();
//submit the form
console.log('form submitted by button click')
});
$('#cancel').click(function(e) {
e.preventDefault();
//close form?
console.log('cancelled form');
});
$('#text').change(function(){
//submit the form
console.log('form submitted, maybe hide form now?');
});
});
The jsFiddle.
Keep submit flag to prevent duplication on form submit. Something like that
<form id="form">
<input id="text" onblur="submitForm('form');">
<button onclick="submitForm('form');">submit</button>
<button type="cancel">cancel</button>
</form>
<script>
var submitFlag = {};
function submitForm(id){
if(!submitFlag[id]){
submitFlag[id] = true;
$('#' + id).submit();
} else {
// do nothing
// alert('Form already submitted');
return;
}
}
</script>
I am developing a web application in which I have created a form and in the onSubmit event of the form I have called a java script function which will check for "Field must not left blank" condition if it is left blank the form must not be posted so I have written following code:
Jsp Page Form
<form action="Result.jsp" name="validityCheck" onsubmit="return fnCheckEmptyField();">
<input type="text" name="txtIIDN" id="txtIIDN" style="font-size:medium;" onkeypress="return fnKeyPress(event)"/>
<input type="submit" id="btnValidityCheck" value="Check Validity" />
</form>
Javascript code is as follows
<script>
function fnCheckEmptyField()
{
var strDomain= document.getElementsByName("txtIIDN").value;
if(strDomain == null)
{
document.getElementById("lblValidityStatus").innerHTML="";
document.getElementById("lblValidityStatus").innerHTML="Domain Name Field Can't be Left Blank";
return false;
}
else
{
return true;
}
}
</script>
When I try to submit the form the javascript function gets executed and then form will gets posted whether the return value is true or false.
I dont want to submit the form when the return value is false i.e when field is empty
and also the condition in onsubmit event onsubmit="return fnCheckEmptyField(); showing me an error
Cannot return from outside a function or method.
Can you figure out what is the mistake I am commiting and possible solution for that?
Remove the onsubmit event on the form and In the submit button HTML, put this
<input type="submit" id="btnValidityCheck" value="Check Validity" onclick="return fnCheckEmptyField();" />
Adding on to what Furqan said, you should probably use the button and an onclick event to check the form, and if the form is valid, then call .submit() on your form.
I tend to not even use a submit button, I just have a normal button, and have it run javascript... then the javascript submits the form.
Example (HTML submit button):
<button id="btnValidityCheck" value="Check Validity" onclick="return fnCheckEmptyField();">Click Here</button>
Javascript function:
function fnCheckEmptyField() {
var strDomain= document.getElementsByName("txtIIDN").value;
if(strDomain == "") {
document.getElementById("lblValidityStatus").innerHTML="Domain Name Field Can't be Left Blank";
}
else {
document.getElementById("myFormId").submit();
}
}
Notice that if the javascript feel that the form is valid, THEN it will submit your form. You need to define an id for your form tag for this to work.
Also, I changed the == null check to == ""... that may have been causing issues...
Cheers!