I have a form that looks like this.
What I want to do is, I want to make sure that Minimum Age < Maximum Age
and Minimum Skill Level < Maximum Skill Level
What is a way to put constraints on the inputs?
HTML
{% extends "template.html" %}
{% set active_page = "events" %}
{% block content %}
<div class="jumbo">
<h2>EVENTS PAGE</h2>
<br/>
<p>Click here to go to the welcome page</p>
</div>
<form action = "/events_success" method=post>
Event Name:<br>
<input type="text" name="eventname" required="">
<br>
Sports:<br>
<select name="sports">
{% for item in sports_data %}
<option value="{{ item[0] }}">{{ item[0] }}</option>
{% endfor %}
</select>
<br>
Location:<br>
<select name="location">
{% for item in loc_data %}
<option value="{{ item[0] }}">{{ item[0] }}</option>
{% endfor %}
</select>
<br>
Time:<br>
<input type="datetime-local" name="time" required>
<br>
Number of Players:<br>
<input type="text" name="numplayers" required>
<br>
Minimum Age:<br>
<input type="text" name="minage" required>
<br>
Maximum Age:<br>
<input type="text" name="maxage" required>
<br>
Minimum Skill Level:<br>
<input type = "radio" name = "minskill" value = "1" required> 1
<input type = "radio" name = "minskill" value = "2"> 2
<input type = "radio" name = "minskill" value = "3"> 3
<input type = "radio" name = "minskill" value = "4"> 4
<input type = "radio" name = "minskill" value = "5"> 5
<br>
Maximum Skill Level:<br>
<input type = "radio" name = "maxskill" value = "1" required> 1
<input type = "radio" name = "maxskill" value = "2"> 2
<input type = "radio" name = "maxskill" value = "3"> 3
<input type = "radio" name = "maxskill" value = "4"> 4
<input type = "radio" name = "maxskill" value = "5"> 5
<br>
<br>
Creator: {{ session['username'] }}<br>
<input type="submit" value="Send">
</form>
{% endblock %}
You can just try some javaScript to prevent the form submission if those fields fails to fulfill that condition. Please check my demo.
**Note: It's just a demo. That's why didn't put any authentication.
window.onload = function() {
let submit = document.querySelector("#submit");
function validateAge(minAge, maxAge) {
let min = parseInt(minAge, 10);
let max = parseInt(maxAge, 10);
return min >= max ? false : true;
}
function validateSkill(minSkill, maxSkill) {
let min = parseInt(minSkill, 10);
let max = parseInt(maxSkill, 10);
return min >= max ? false : true;
}
submit.onclick = function() {
let minAge = document.querySelector("#minage").value;
let maxAge = document.querySelector("#maxage").value;
let radios = document.querySelectorAll('input[type="radio"]:checked');
let minSkill = radios[0].value;
let maxSkill = radios[1].value;
if(validateAge(minAge, maxAge) && validateSkill(minSkill, maxSkill))
return true;
else {
alert("Invalid data");
return false;
}
}
}
<form id="myForm" action = "/events_success" method=post >
Minimum Age:<br>
<input type="text" name="minage" id="minage" required>
<br>
Maximum Age:<br>
<input type="text" name="maxage" id="maxage" required>
<br>
Minimum Skill Level:<br>
<input type = "radio" name = "minskill" value = "1" required> 1
<input type = "radio" name = "minskill" value = "2"> 2
<input type = "radio" name = "minskill" value = "3"> 3
<input type = "radio" name = "minskill" value = "4"> 4
<input type = "radio" name = "minskill" value = "5"> 5
<br>
Maximum Skill Level:<br>
<input type = "radio" name = "maxskill" value = "1" required> 1
<input type = "radio" name = "maxskill" value = "2"> 2
<input type = "radio" name = "maxskill" value = "3"> 3
<input type = "radio" name = "maxskill" value = "4"> 4
<input type = "radio" name = "maxskill" value = "5"> 5
<br>
<br>
<input type="submit" value="Send" id="submit">
</form>
You can use some common PHP validation techniques. These are more beneficial as less risky to injection and other exploits.
PHP:
if (empty($_POST["code"])) {
$errcode = "Verify that you're human";
} else {
$code = test_input($_POST["code"]);
if (!$_POST['code'] < $string) {
$errcode = "Make code greater than string";
}
}
HTML:
<p><label for="code">Scrambled code: </label><input type="text" name="code" id="code" /><span class="error"> * <?php echo $errcode;?></span></p>
Related
I'm making a form for a task in class and nobody can figure out why the input validation isn't working. The code for the whole form is below. It's specifically the required tag that seems to be causing problems.
<form action = "">
<p>First Name</p>
<input type = "text" name = "firstname" size = "25" maxlength = "20"></input> <br>
<p>Last Name</p>
<input type = "text" name = "lastname" size = "25" maxlength = "20"></input> <br>
<p>Age</p>
<input type = "radio" name = "age" value = "<25" checked></input><25
<input type = "radio" name = "age" value = "25-40" ></input>25-40
<input type = "radio" name = "age" value = ">40" ></input>>40
<input type = "radio" name = "age" value = "Rather not say" ></input>Rather not say <br>
<div style = "float:left">
<p>Favourite food</p>
<select name = "Favourite food" style = "float:left;">
<option value = "1" selected>Italian</option>
<option value = "2">Indian</option>
<option value = "3">Thai</option>
<option value = "4">Chinese</option>
</select>
</div>
<br>
<div style = "float:right">
<p>What new country would you like to see on the website?</p>
<select name = "newCountry">
<option value = "1" selected>Vietnamese</option>
<option value = "2">American</option>
<option value = "3">French</option>
<option value = "4">Spanish</option>
</select>
</div>
<br>
<p style = "clear:both; float:left;">What new cooking technique would you like to see on the website?</p>
<textarea name = "NewTechnique" rows = "5" cols = "100" maxlength = "500" style = "clear:both;"> </textarea>
<br>
<p>Please rate the following from 1 (poor) to 5 (excellent)</p>
<p>Ease of use</p>
<input type = "text" name = "easeofuse" size = "25" min = "1" max = "5" required=""></input> <br>
<p>Quality of information</p>
<input type = "text" name = "infoquality" size = "25" min = "1" max = "5" required></input> <br>
<p>Taste of recipies</p>
<input type = "text" name = "tastes" size = "25" min = "1" max = "5" required = ""></input> <br>
<p>Permission for my information to be held and used for future developments</p>
<input type ="radio" name = "permission" value = "1" required></input>Yes
<input type ="radio" name = "permission" value = "2"></input>No
<input type = "submit" onclick="alert('form entered')" value = "submit"></input>
</form>
I can't see what the problem is. Is it because the action tag is empty? We've been told to leave it like that.
Edit:Problem has been solved!
To get the form required fields to work, I've replaced the onclick with onsubmit inside the form tag. I've also cleaned up whatever HTML oddities I found after a quick skim (I've probably missed a few).
<form onsubmit='alert("ya done clicked")'>
<p>First Name</p>
<input type="text" name="firstname" size="25" maxlength="20"> <br>
<p>Last Name</p>
<input type="text" name="lastname" size="25" maxlength="20"> <br>
<p>Age</p>
<input type="radio" name="age" value="<25" checked><25
<input type="radio" name="age" value="25-40">25-40
<input type="radio" name="age" value=">40">>40
<input type="radio" name="age" value="Rather not say">Rather not say <br>
<div style="float:left">
<p>Favourite food</p>
<select name="Favourite food" style="float:left;">
<option value="1" selected>Italian</option>
<option value="2">Indian</option>
<option value="3">Thai</option>
<option value="4">Chinese</option>
</select>
</div>
<br>
<div style="float:right">
<p>What new country would you like to see on the website?</p>
<select name="newCountry">
<option value="1" selected>Vietnamese</option>
<option value="2">American</option>
<option value="3">French</option>
<option value="4">Spanish</option>
</select>
</div>
<br>
<p style="clear:both; float:left;">What new cooking technique would you like to see on the website?</p>
<textarea name="NewTechnique" rows="5" cols="100" maxlength="500" style="clear:both;"> </textarea>
<br>
<p>Please rate the following from 1 (poor) to 5 (excellent)</p>
<p>Ease of use</p>
<input type="text" name="easeofuse" size="25" min="1" max="5" required> <br>
<p>Quality of information</p>
<input type="text" name="infoquality" size="25" min="1" max="5" required> <br>
<p>Taste of recipies</p>
<input type="text" name="tastes" size="25" min="1" max="5" required> <br>
<p>Permission for my information to be held and used for future developments</p>
<input type="radio" name="permission" value="1" required>Yes
<input type="radio" name="permission" value="2">No
<input type="submit" value="submit">
</form>
If you name an input from the user in one html file using and tags, how do you use it in another html file?? I want to use only html. No javascript or anything.
The main file:
<HTML>
<HEAD>
<TITLE>
Harry Potter
</TITLE>
</HEAD>
<BODY text = "red" bgcolor = "green">
<CENTER><H1><B><U>Harry Potter</U></B></H1></CENTER>
Please register for HPhmR by <A href = form.html>Clicking Here</A>
</BODY>
</HTML>
Form.html:
<HTML>
<HEAD>
<TITLE>
HPhmR Resgistering
</TITLE>
</HEAD>
<BODY bgcolor = "turquoise">
<FORM action = "harry potter.html" method = "post" name = "HPhmR">
<P>Please enter your username :
<INPUT type = "text" size = "30" name = "username" maxlength = "20">
</P>
<P>Please enter your password :
<INPUT type = "password" size = "30" name = "password" maxlength = "15">
</P>
<P>Please choose your favourite character:<BR>
<SELECT name = "favcha" size = "8" MULTIPLE>
<OPTION value = "Dumbledore">Albus Dumbledore</OPTION>
<OPTION value = "Voldemort">Lord Voldemort</OPTION>
<OPTION value = "Harry">Harry Potter</OPTION>
<OPTION value = "Ron">Ron Weasely</OPTION>
<OPTION value = "Hermione">Hermione Granger</OPTION>
<OPTION value = "Sirius">Sirius Black</OPTION>
<OPTION value = "Bellatrix">Bellatrix Lestrange</OPTION>
<OPTION value = "Draco">Draco Malfoy</OPTION>
</SELECT>
</P>
<P>Select your age range :
<INPUT type = "radio" name = "10-15" value = "1">10 to 15 yrs
<INPUT type = "radio" name = "15-20" value = "2">15 to 20 yrs
<INPUT type = "radio" name = "20-30" value = "3">20 to 30 yrs
<INPUT type = "radio" name = "30-45" value = "4">30 to 45 yrs
<INPUT type = "radio" name = "45-60" value = "5">45 to 60 yrs
<INPUT type = "radio" name = "60+" value = "6">Above 60 yrs
</P>
<INPUT type = "submit" value = "Submit and Register">
</FORM>
</BODY>
</HTML>
How do I use the input from the user in form.html? I want to use that input and do something else in the main file.
You can not do that using just html.but you can use php or asp.net to send forms from one file to another.
I want to make a PHP variable the value of a hidden form input. The form is inside of my PHP (I'm echoing the form), and nothing that I have tried works.
Here's my code:
echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<!-- Here is where I need to make my PHP variable the value: -->
<input type = "text" name = "referer" style = "display: none" value = "$variable">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';
Try this:
?><!-- exit out of php into html-->
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<!--Here is where I need to make my PHP variable the value:-->
<input type = "text" name = "referer" style = "display: none" value = "<?=$variable?>">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
<?php // enter back into php
The <?= ?> is a php short tag
Also, if you still want to use echo, try this:
//note: I changed the quotes
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'>
<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";
See this Q/A for more info
The usual string replacements "$var" don't work here , as it's all contained within a single quote string which doesn't allow for string replacements. You will have to concatenate manually
echo '
<div id = "login">
<form action = "process.php" method = "POST">
Name: <input type = "text" name = "name" required>
<input type = "text" name = "referer" style = "display: none" value = "' . $variable . '">
<input type = "submit" name = "submit" value = "Enter">
</form>
</div>
';
Try this sample here https://eval.in/506642
echo "
<div id = 'login'>
<form action = 'process.php' method = 'POST'>
Name: <input type = 'text' name = 'name' required>
//Here's where I need to make my PHP variable the value:
<input type = 'text' name = 'referer' style = 'display: none' value = '$variable'>
<input type = 'submit' name = 'submit' value = 'Enter'>
</form>
</div>
";
i have a bootstrap site and have 4 different forms. I set the required attribute against certain fields. On my local machine, the validation works if the fields are left blank but when I post it to a web host, the validation does not work.
The field types range from free text input to check boxes, select boxes and radio buttons.
I am writing this from my mobile so don't have a snippet of code to show. Sorry all. Suggestions are more than welcome.
<fieldset>
<b><u><h4>Trip Type</h4></u></b>
<div class = "form-group">
<label for="where">Will you be:</label>
<select class="form-control" id="where" name = "where" required>
<option></option>
<option value = "travelling to">Travelling to the Airport</option>
<option value = "travelling from">Travelling from the Airport</option>
</select>
</div>
<div class = "row">
<div class = "col-lg-6 col-md-6">
<div class = "form-group">
<label for = "airporttype">From / to which airport:</label>
<div class = "radio">
<label class="radio-inline"><input type="radio" name="airporttype" id = "airporttype" value = "International" required>International</label>
<label class="radio-inline"><input type="radio" name="airporttype" id = "airporttype" value = "Domestic">Domestic</label>
</div>
</div>
</div>
<div class = "col-lg-6 col-md-6">
<div class="control-group form-group">
<label for = "trip">Is this a Single or Return Trip:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "trip" id = "trip" class = "return" value = "Single" required>Single</label>
<label class="radio-inline"><input type = "radio" name = "trip" id = "trip" class = "return" value = "Return">Return Trip</label>
</div>
</div>
</div>
</div>
<hr>
<b><u><h4>Your Details</h4></u></b>
<div class="control-group form-group">
<div class="controls">
<label for = "fname">First Name:</label>
<input type = "text" class = "form-control" id = "fname" name = "fname" required placeholder = "Enter First Name" data-validation-required-message="Please enter your first name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "surname">Last Name:</label>
<input type="text" class="form-control" id="surname" name = "surname" required placeholder = "Enter Last Name" data-validation-required-message="Please enter your last name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "cell1">Contact Mobile / Cell:</label>
<input type="text" class="form-control" id="cell1" name = "cell1" required placeholder = "Enter Mobile / Cell Number Main" data-validation-required-message="Please enter your main contact number.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "cell2">Contact Mobile / Cell 2:</label>
<input type="text" class="form-control" id="cell2" name = "cell2" placeholder = "Enter Mobile / Cell Number Alt" data-validation-required-message="Please enter your alternate contact number.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "email">Enter your email address:</label>
<input type = "email" class = "form-control" id = "email" name = "email" required placeholder = "you#yourdomain.com" data-validation-required-message="Please enter your email address.">
<p class="help-block"></p>
</div>
</div>
<br>
<div class="control-group form-group">
<div class="controls">
<label for = "date16">Travel Date:</label>
<input type = "text" class="form-control datepicker" id = "date16" name = "date16" required data-validation-required-message="Please enter your travel date." style = "text-align: center">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for="address">Pick Up / Destination Address:</label>
<input type = "text" class = "form-control" id = "address" name = "address" placeholder = "The start or end of your journey" required data-validation-required-message = "Please enter the address where you will start or finish your journey.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "passnum">Number of Passengers:</label>
<select class="form-control" id = "passnum" name = "passnum" required>
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
<option value = "6">6</option>
<option value = "7">7</option>
<option value = "8">8</option>
<option value = "9">9</option>
<option value = "10">10</option>
<option value = "11">11</option>
<option value = "12">12</option>
<option value = "13">13</option>
<option value = "14">14</option>
<option value = "15">15</option>
<option value = "16">16</option>
<option value = "17">17</option>
<option value = "18">18</option>
<option value = "19">19</option>
<option value = "20">20</option>
<option value = "21">21</option>
<option value = "22">22</option>
<option value = "23">23</option>
<option value = "24">24</option>
</select>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label for = "flightnum">Flight Number:</label>
<input type = "text" class="form-control" id = "flightnum" name = "flightnum" placeholder = "Please enter your flight number" required data-validation-required-message = "Please enter your flight number.">
<p class="help-block"></p>
</div>
</div>
<hr>
<b><u><h4>Child Seats</h4></u></b>
<div class = "row">
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "babyseat">Baby Seat Required:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "babyseat" id = "babyseat" class = "bseat12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "babyseat" id = "babyseat" class = "bseat12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "babyseat1">
<div class="controls">
<label for = "bseatnum">Number of Baby Seats:</label>
<select class="form-control" id = "bseatnum" name = "bseatnum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "boost">Booster Seat Required:</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "boost" id = "boost" class = "boost12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "boost" id = "boost" class = "boost12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "boost1" >
<div class="controls">
<label for = "boostnum">Number of Booster Seats:</label>
<select class="form-control" id = "boostnum" name = "boostnum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
<div class = "col-lg-4">
<div class="control-group form-group">
<label for = "cradle">Baby Cradle Required</label>
<div class = "radio">
<label class="radio-inline"><input type = "radio" name = "cradle" id = "cradle" class = "cradle12" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type = "radio" name = "cradle" id = "cradle" class = "cradle12" value = "No">No</label>
</div>
</div>
<div class="control-group form-group" id = "cradle1">
<div class="controls">
<label for = "cradlenum">Number of cradles:</label>
<select class="form-control" id = "cradlenum" name = "cradlenum">
<option></option>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
<option value = "4">4</option>
<option value = "5">5</option>
</select>
</div>
</div>
</div>
</div>
<hr>
<b><u><h4>Additional Services</h4></u></b>
<div class = "row">
<div class = "col-lg-6">
<div class="control-group form-group">
<label for = "shuttle">Shuttle Service:</label>
<div class="radio">
<label for = "shuttle" class="radio-inline"><input id = "shuttle" name = "shuttle" type="radio" value = "Yes" required>Yes</label>
<label for = "shuttle" class="radio-inline"><input id = "shuttle" name = "shuttle" type="radio" value = "No">No</label>
<p class="help-block"></p>
</div>
</div>
</div>
<div class = "col-lg-6">
<div class="control-group form-group">
<label for "charter">Private Charter:</label>
<div class="radio">
<label class="radio-inline"><input type="radio" name="charter" id = "charter" value = "Yes" required>Yes</label>
<label class="radio-inline"><input type="radio" name="charter" id = "charter" value = "No">No</label>
<p class="help-block"></p>
</div>
</div>
</div>
</div>
<hr>
</fieldset>
validation only using required attribute is not good, because it may not work in many cases. for example when using javascript submit() method, also in some browsers,required wont work. So Best way is to validate using javascript(client side)
So create a javascript function and call it during form onsubmit event
function checkform(form) {
var inputs = form.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if(inputs[i].hasAttribute("required")){
if(inputs[i].value == ""){
alert("Please fill all required fields");
return false;
}
}
}
return true;
}
Here is my HTML code. In the browser, I click on the text fields and a box shows for file input. It is like I selected the type = "file" input field. Why does my HTML code do this?
<div id = "section">
<form action = "receive.php" method = "post">
<!-- Image to upload -->
<label for = "item"> Item: <input id = "item" type = "file" name = "items" accept = "image/*">
<!-- Text to enter -->
<label for = "mail"> Email: <input id = "mail" type = "text" name = "email">
<label for = "word"> Words: <input id = "word" type = "text" name = "words">
<input type = "submit" value = "Submit" name = "submit">
</form>
</div>
You need to close your <label> tags.
Otherwise, your entire form is in the <label> for the upload control, and clicking on the label will click the control.
You haven't closed your elements properly, most likely the <label> elements.
<div id = "section">
<form action = "receive.php" method = "post">
<!-- Image to upload -->
<label for = "item"> Item: <input id = "item" type = "file" name = "items" accept = "image/*"></label>
<!-- Text to enter -->
<label for = "mail"> Email: <input id = "mail" type = "text" name = "email"></label>
<label for = "word"> Words: <input id = "word" type = "text" name = "words"></label>
<input type = "submit" value = "Submit" name = "submit">
</form>
</div>