I am using Formspree (which is awesome) except: I have set all my fields to required, when I try manually I can't submit a form without filling everything properly, and however I still get a bunch of blank forms coming through. Has anybody experienced / fixed this before?
Thanks!
<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">
<input class="form-control input-lg" name="firstname" placeholder="First Name" required type="text">
<input class="form-control input-lg" name="lastname" placeholder="Last name" required type="text">
<input class="form-control input-lg" name="phone" placeholder="Phone number" required type="text">
<select required name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
<option value="" disabled selected>Please select your country </option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="other">Not in the List</option>
<!-- etc -->
</select>
<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />
<button id="demo" type="submit">GO</button>
And this is another Solution for your code, as this will work even if somebody put "spaces or leave it with blank" in the field of Name or Phone.
Check the Fiddle..
// Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {
// Get the Login Name value and trim it
var fname = $.trim($('#fname').val());
var lname = $.trim($('#lname').val());
var phon =$.trim($('#phon').val());
// Check if empty of not
if (fname === '') {
alert('First name is empty.');
return false;
}
else if (lname === '') {
alert('Last Name is empty.');
return false;
}
else if (phon === '') {
alert('Phone is empty.');
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">
<input class="form-control input-lg" name="firstname" placeholder="First Name" id="fname" required type="text">
<input class="form-control input-lg" name="lastname" placeholder="Last name" id="lname" required type="text">
<input class="form-control input-lg" name="phone" placeholder="Phone number" id="phon" required type="text">
<select name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
<option value="" disabled selected>Please select your country </option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="other">Not in the List</option>
<!-- etc -->
</select>
<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />
<button id="demo" type="submit">GO</button>
If somebody type space to the input box then this Jquery code will work and ask him to fill proper text in fields.
Try W3Schools example of
http://www.w3schools.com/tags/att_input_required.asp
<input class="form-control input-lg" name="firstname" placeholder="First Name" required >
with no equals or anything
I thought the required tag was
required="True/false"
A simple solution would be something like this.
You can do this by using JQuery
// Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {
// Get the Login Name value and trim it
var name = $.trim($('#log').val());
// Check if empty of not
if (name === '') {
alert('Text-field is empty. You cannot leave is blank!');
return false;
}
});
.text-label {
color: #cdcdcd;
font-weight: bold;
}
#pwd{
margin:10px 18px;
}
#logBtn{
margin:10px 90px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="login.php" method="post">
<label>Login Name:</label>
<input type="text" name="email" id="log" />
<br/>
<label>Password:</label>
<input type="password" name="password" id="pwd" />
<br/>
<input type="submit" id="logBtn" name="submit" value="Login" />
</form>
Related
I'm using this jQuery script to focus onto next input when enter pressed:
$(function() {
$(".form >input").on('keyup', function(e) {
if (e.which === 13) {
$(this).next('input').focus();
}
});
});
It does work with this basic skinny HTML form
<div class="form">
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
</div>
But not with this one
<div class="form">
<input name="cislo1" type="text" class=cislo1 placeholder="" id="cislo1" autofocus /><br>
<input name="cislo2" type="text" class=cislo2 placeholder="" id="cislo2" /><br>
<input name="cislo3" type="text" class=cislo3 placeholder="" id="cislo3" /><br>
<input name="cislo4" type="text" class=cislo4 placeholder="" id="cislo4" /><br>
<input name="cislo5" type="text" class=cislo5 placeholder="" id="cislo5" /><br>
<input name="cislo6" type="text" class=cislo6 placeholder="" id="cislo6" /><br>
<input name="cislo7" type="text" class=cislo7 placeholder="" id="cislo7" /><br>
<input name="cislo8" type="text" class=cislo8 placeholder="" id="cislo8" /><br>
<input name="cislo9" type="text" class=cislo9 placeholder="" id="cislo9" /><br>
<input name="cislo10" type="text" class=cislo10 placeholder="" id="cislo10" /><br>
<input name="cislo11" type="text" class=cislo11 placeholder="" id="cislo11" /><br>
</div>
What seems to be the problem here? Help me please, I'm stuck
The issue here is you are using direct child selector like:
$(".form > input")
This only matches any direct input element inside form class, but in your next example input is not the direct child. So, you need to descent sector instead like:
$(".form input")
This will match any input inside the form class where it is a direct child or not.
Also, you need to replace
$(this).next('input').focus();
with this:
$(this).nextAll('input:first').focus();
This is needed because .next() will get the immediately following sibling of each element, but here input is not the immediate sibling. We also have some <br> in between which are causing the issue. This can be resolved using .nextAll('input:first') instead.
Demo:
$(function() {
$(".form input").on('keyup', function(e) {
if (e.which === 13) {
$(this).nextAll('input:first').focus();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form">
<p class="cislo">
<input name="cislo1" type="text" class=cislo1 placeholder="" id="cislo1" autofocus onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo1.value=cislo1.value.slice(0,2)" /><br>
<input name="cislo2" type="text" class=cislo2 placeholder="" id="cislo2" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo2.value=cislo2.value.slice(0,2)" /><br>
<input name="cislo3" type="text" class=cislo3 placeholder="" id="cislo3" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo3.value=cislo3.value.slice(0,2)" /><br>
<input name="cislo4" type="text" class=cislo4 placeholder="" id="cislo4" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" oninput="cislo4.value=cislo4.value.slice(0,2)" /><br>
</p>
</div>
I'm just getting started with web design and it may be a silly question. I haven't added JavaScript code yet, but what I need is that all the options/forms require input before the query is submitted, otherwise to get highlighted in red. Any help would be great.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
<label for="value1">Example1</label>
<select id="value1" required>
<option value="">--Select an option--</option>
<option value="opt1">1</option>
<option value="opt2">2</option>
</select>
<label for="value2">Example2</label>
<select id="value2" required>
<option value="">--Select an option--</option>
<option value="Normal">Normal</option>
<option value="Superior">Superior</option>
</select>
</form>
<label for="date1">date1</label>
<input type="date" id="firstDate" required>
<label for="date2">date2</label>
<input type="date" id="secondDate" required>
<label for="value3">example3</label>
<input type="number" id="exampleNumber" required>
<button type="button" id="button1" required>button1</button>
</body>
</html>
Are you expecting like this:
The below given code can be used to validate each and each field more specifically.
Also i have added :focus selector in order to highlight if the input data is not given.If the input data is not given it will be highlighted in red.
function validate(){
if(document.rform.fname.value==""){
alert("Enter Your First Name");
document.rform.fname.focus();
return false;
}
if(document.rform.lname.value==""){
alert("Enter Your Last Name");
document.rform.lname.focus();
return false;
}
if(document.rform.address.value==""){
alert("Enter Your address");
document.rform.address.focus();
return false;
}
var val = document.getElementById('mobilenumber').value;
if (val.length < 10 || val.length > 12 ) {
alert("Enter a valid phone number")
return false; // keep form from submitting
}
if(document.rform.city.value==""){
alert("Enter your city name");
document.rform.city.focus();
return false;
}
if(document.rform.pincode.value==""){
alert("Enter a valid pin number");
document.rform.pincode.focus();
return false;
}
if(document.rform.email.value==""){
alert("Enter your Mail Id");
document.rform.email.focus();
return false;
}
var mailformat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(document.rform.email.value.match(mailformat))
{
document.rform.email.focus();
return true;
}
else
{
alert("You have entered an invalid email address!");
document.rform.email.focus();
return false;
}
return true;
}
.inputs:focus{
box-shadow: 0 0 10px #ff0000;
border-color: #ff0000;
outline: none !important;
}
<form method="POST" name="rform" action=" " onsubmit="return(validate());">
<label for="right-label" class="right inline">First Name</label>
<input class="inputs" type="text" id="right-label" placeholder="First Name" name="fname" requried>
<br>
<label for="right-label" class="right inline">Last Name</label>
<input class="inputs" type="text" id="right-label" placeholder="Last Name" name="lname" requried>
<br>
<label for="right-label" class="right inline">Address</label>
<input class="inputs" type="text" id="right-label" placeholder="Address" name="address" requried>
<br>
<label for="right-label" class="right inline">City</label>
<input class="inputs" type="text" id="right-label" placeholder="City" name="city" requried>
<br>
<label for="right-label" class="right inline">Pin Code</label>
<input class="inputs" type="number" id="right-label" placeholder="Pincode" name="pincode" requried>
<br>
<label for="right-label" class="right inline">E-Mail</label>
<input class="inputs" type="email" id="right-label" placeholder="Mail Id" name="email" requried>
<br>
<label for="right-label" class="right inline">Mobile Number</label>
<input class="inputs" type="number" id="mobilenumber" placeholder="Mobile Number" name="mobile_num" requried>
<br>
<input type="submit" id="right-label" value="Register" name="submit" >
<input type="reset" id="right-label" value="Reset">
</form>
You can add hidden error class below every input fields. You can validate the form using javascript and jquery. If the input is wrong you can make the error class appear.
HTML :
<form method="POST" id="register_form" action=" " autocomplete="off">
<label for="right-label" class="right inline">First Name</label>
<input class="inputs" type="text" id="firstName" placeholder="First Name">
<small id="fn_error" class="form-text text-muted"></small>
<br>
<label for="right-label" class="right inline">Last Name</label>
<input class="inputs" type="text" id="lastName" placeholder="Last Name">
<small id="ln_error" class="form-text text-muted"></small>
<br>
<input type="submit" id="right-label" value="Register" name="submit" >
<input type="reset" id="right-label" value="Reset">
</form>
JS :
$('#register_form').on("submit",function () {
var firstName= $("#firstName");
var lastName= $("#lastName");
var status = false;
if (firstName.val() == "") {
firstName.addClass("borer-danger");
$("#fn_error").html("<span class='text-danger'>Please Enter Email</span>");
status = false;
} else {
firstName.removeClass("border-danger");
$("#fn_error").html("");
status = true
}
if (lastName.val() == "") {
lastName.addClass("borer-danger");
$("#ln_error").html("<span class='text-danger'>Please Enter Last Name</span>");
status = false;
} else {
lastName.removeClass("border-danger");
$("#ln_error").html("");
status = true
}
if (status == true) {
alert("Ready");
}
})
I am using an external program, "Autopilot" to essentially capture my form (For my marketers). However, I have made a form that does not require any "action" on post, eg, I don't want the page to go anywhere, instead I want #answer-button to act as a button which goes to the next question. (I have buttons before the form which go to the next question).
So my question is, how do I go about creating a form that doesn't reload the page, but still posts data using " action='something' "?
my code
<form id="email-gate-form" action="">
<input type="text" placeholder="FIRST NAME" name="first-name" required>
<input type="text" placeholder="LAST NAME" name="last-name" required>
<input type="text" placeholder="EMAIL" name="email" required>
<input type="text" placeholder="COMPANY" name="company" required>
<input type="submit" value="GET MY RESULTS" name="submit" id="answer-button" class="question-form-gate">
</form>
A small peak into my javascript
$("#answer-button").click(function(e) {
e.preventDefault();
});
var emailGateButton = $("#email-gate .question-form-gate");
emailGateButton.on('click', function () {
questionsOverlay.show();
TweenMax.staggerTo("#email-gate .animated", 0.25, {opacity: 0}, 0.25, "-=0.5")
setTimeout(function(){
emailGate.hide();
},1000)
setTimeout(function(){
questionFinal.show();
TweenMax.staggerFrom("#section-final .animated", 0.25, {opacity: 0}, 0.25, "-=0.5")
},1000)
setTimeout(function(){
questionsOverlay.hide();
},3000);
});
any help greatly appreciated.
<form id="email-gate-form" method="post">
<input type="text" placeholder="FIRST NAME" name="first-name" required>
<input type="text" placeholder="LAST NAME" name="last-name" required>
<input type="text" placeholder="EMAIL" name="email" required>
<input type="text" placeholder="COMPANY" name="company" required>
<input type="submit" value="GET MY RESULTS" name="submit" id="answer-button" class="question-form-gate">
</form>
$("#email-gate-form").submit(function(e) {
e.preventDefault();
process_form();
});
function process_form() {
var name = $("input[name=first-name]").val();
console.log(name);
// do whatever you need to do with your varables
}
In my html I have a combo box which list all the available usertypes and then the input varies by what the user selects on the combo box so you can see that I have my onchange.
But my question is how would I be able to access the value of the selected combo box? Is it possible to get its selected value even it is not a part of the form? I've already tried to have only one form that is
<form action="" name="form1" method="post">
<select id="usertype" name="usertype" class="dropdown-select" onfocus="getPrevious()" onchange="set()" >
<option value="1" selected="selected">Chairperson</option>
<option value="2">Dean</option>
<option value="3">Faculty</option>
<option value="4">Staff</option>
<option value="6">Admin</option>
</select>
<div id="formpage_Chairperson" style="visibility: visible; display: block;">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="text" value="" name="E" />
<input type="text" value="" name="F" />
<input type="Submit" value="Submit Form" name="submitForm" />
</div>
<div id="formpage_Chairperson" style="visibility: hidden; display: none;">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="Submit" value="Submit Form" name="submitForm" />
</div>
</form>
But when I clicked the Submit Form nothing happens. You can see that I'm choosing only what to display because different usertypes (e.g. Chairperson, Dean, etc) have also different inputs. So I make different forms for each particular usertypes. But then it throws an error ' Undefined index: usertype ' and I know that is because it's not part of the form.
So can you give me some advice on how will I be able to come up with this properly? Thank you.
Here is my html code:
<select id="usertype" name="usertype" class="dropdown-select" onfocus="getPrevious()" onchange="set()" >
<option value="1" selected="selected">Chairperson</option>
<option value="2">Dean</option>
<option value="3">Faculty</option>
<option value="4">Staff</option>
<option value="6">Admin</option>
</select>
<form action="" name="form1" method="post">
<div id="formpage_Chairperson" style="visibility: visible; display: block;">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="text" value="" name="E" />
<input type="text" value="" name="F" />
<input type="Submit" value="Submit Form" name="submitForm" />
</div>
</form>
<form action="" name="form1" method="post">
<div id="formpage_Dean" style="visibility: hidden; display: none;">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="Submit" value="Submit Form" name="submitForm" />
</div>
</form>
This is the Javascript for handling the onchange event of the combo box:
var prev = ""
function pagechange(topage) {
var page;
if (prev.length > 0) {
page = document.getElementById('formpage_' + prev);
if (page)
page.style.visibility='hidden';
page.style.display = 'none';
}
page = document.getElementById('formpage_' + topage);
if (!page)
return false;
page.style.visibility='visible';
page.style.display = 'block';
prev = topage;
return true;
}
function set(){
var e = document.getElementById("usertype");
var selection = e.options[e.selectedIndex].text;
pagechange(selection);
}
Not sure how you wanted to "access" it. I'm assuming you mean with the rest of the form data, so you could do something like the following:
function pagechange(topage) {
var page;
if (prev.length > 0) {
page = document.getElementById('formpage_' + prev);
if (page) {
page.style.visibility='hidden';
page.style.display = 'none';
}
page = document.getElementById('formpage_' + topage);
// create hidden input field and append to form
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", "page");
input.setAttribute("value", topage);
page.appendChild(input);
// end of changes
if (!page)
return false;
page.style.visibility='visible';
page.style.display = 'block';
prev = topage;
}
return true;
}
Also, you were missing a closing bracket on your if (prev.length > 0), so I closed it right before the return true;. You may need to adjust depending on your logic.
Hope this gets you close.
I'm trying to post two different sets of mixed values. Each form will share some user submitted text like name and email.
<form method="post" action="url1">
<input type="hidden" name="donate" value="food">
<input type="hidden" name="days" value="30">
<input type="text" name="cans" value="5">
<input type="text" name="full_name" value="">
<input type="text" name="email" value="">
<input type="submit" name="submit" value="join1"/>
</form>
<form method="post" action="url1">
<input type="hidden" name="donate" value="shoes">
<input type="text" name="pairs" value="">
<input type="text" name="style" value="">
<input type="text" name="full_name" value="">
<input type="text" name="email" value="">
<input type="submit" name="submit" value="join2"/>
</form>
<possible dropdown>
It would be ideal to have Dropdown select between the two forms and submit.
Well, using only html you will get a problem as a submit button only submit its own form.
It's also not valid to have forms in other forms.
However, is a trick to accomplish your goal using javascript!
With a little function you can e.g. hide all other forms than one:
function showform(index){
var forms = document.forms;
for(var i=0;i<forms.length;i++)
forms[i].style.display=(i==index?"block":"none");
}
Then you only need to add the onchange-event on your dropdown-list and hide one of the forms by default.
This would result in something like this:
<html>
<head>
<style language="css" type="text/css">
form input{display:block;width:100px;}
</style>
<script language="javascript" type="text/javascript">
function showform(index){
var forms = document.forms;
for(var i=0;i<forms.length;i++)
forms[i].style.display=(i==index?"block":"none");
}
</script>
</head>
<body>
<select onchange='showform(this.selectedIndex);'>
<option>form1</option>
<option>form2</option>
</select>
<form method="post" action="url1">
<input type="hidden" name="donate" value="food"/>
<input type="hidden" name="days" value="30"/>
<input type="text" name="cans" value="5"/>
<input type="text" name="full_name" value=""/>
<input type="text" name="email" value=""/>
<input type="submit" name="submit" value="join1"/>
</form>
<form id='form2' style='display:none' method="post" action="url2">
<input type="hidden" name="donate" value="shoes"/>
<input type="text" name="pairs" value="2"/>
<input type="text" name="style" value=""/>
<input type="text" name="full_name" value=""/>
<input type="text" name="email" value=""/>
<input type="submit" name="submit" value="join2"/>
</form>
</body></html>
If you don't like javascript, you would need to do that in php by using an additional form for the select-element and insert only the wanted form (or hide the others).
I already see some design issues with the way you have planned your multi-form setup, there are much better techniques to handle this - but I will try to help you using your own technique.
By using your own example, this should work for you:
<form id="join1" method="post" action="url1">
<input type="hidden" name="donate" value="food">
<input type="hidden" name="days" value="30">
<input type="text" name="cans" value="5">
<input type="text" name="full_name" value="">
<input type="text" name="email" value="">
</form>
<form id="join2" method="post" action="url1">
<input type="hidden" name="donate" value="shoes">
<input type="text" name="pairs" value="">
<input type="text" name="style" value="">
<input type="text" name="full_name" value="">
<input type="text" name="email" value="">
</form>
<select id="formList">
<option value="">Select form..</option>
<option value="join1">Join1</option>
<option value="join2">Join2</option>
</select>
<button id="submitBtn">Submit</button>
And this is the javascript for making it happen (using jQuery):
$('#submitBtn').on('click',function(){
var selectedForm = $('#formList').val();
if(selectedForm){
$('#'+ selectedForm).submit();
}
});
the example is also available as a jsfiddle:
https://jsfiddle.net/k1jf4b4L/
Hope it helps a bit