Maximum value limit in input realtime html - java

I need to limit the maximum input value in the name = "name" field.
Real-time processing. The <input max = 100> constraint does not work.
How can I do that?
<script>
function showcost() {
document.getElementById("cost").value = document.getElementById('serviceslist').value;
}
function sum2() {
var x = document.getElementById('cost2').value;
var y = document.getElementById('amount2').value;
var total = (parseFloat(x) * parseFloat(y))/100;
document.getElementById("total2").value = total;
}
</script>
<br>
<div class="container">
<div class="row">
<div class="col-md-12 col-md-offset-0">
<form>
<p>Procent:<input name="name" size="3" min=1 max=100 type="number" id="amount2" step=1 id="cost2" pattern="[0-9]" onchange="sum2()" oninput="this.value = this.value.replace(/[^0-9]/g, '').replace(/(\..*)\./g, '$1');" );" ></p>
<p>Результат:<input type="text" id="total2" value="0" disabled></p>
</form>
</div>
</div>
</div>

Related

Why random checkbox choosing giving error,not in serial-wise in JSP page?

I am trying to make a basic shop-billing JSP project. now when i type random product quantity with checkbox it giving error like below image. but when i checked-value with serial wise, it is not giving the error, it giving me the result what i desiring. I have also check it is empty or not. but can't able to get out of this error. how can i get rid out of it?
Code:
<%--
Document : order
Created on : Jun 15, 2019, 3:11:06 PM
Author : Riddhi
--%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*" %>
<!DOCTYPE html>
<html lang="en">
<%# include file="header.jsp" %>
<h2 class="text-center"> ShopBilling </h2>
<p><br/></p>
<%
String Host = "jdbc:mysql://localhost:3306/shopbilling";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
PreparedStatement ps=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(Host, "root", "");
StringBuilder sb = new StringBuilder();
%>
<div class="row justify-content-center">
<div class="col-md-6">
<div class="row">
<div class="col-md-6"><h3>Product Details</h3></div>
<div class="col-md-6 text-right">
Back to Home
</div>
</div>
<p></p>
<form action="" method="post">
<%
String products[] = request.getParameterValues("products");
String items[] = request.getParameterValues("item_no");
int sum=0;
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {
%> <%
statement = connection.createStatement();
String u=request.getParameter("u");
int num=Integer.parseInt(products[i]);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<label for="product_name">Product Name: <%=rs.getString("product_name")%></label>
<input type="hidden" class="form-control" id="product_name" name="product_name" value='<%=rs.getString("product_name")%>'>
</div>
<div class="form-group">
<label for="product_price">Product Single Price: <%=rs.getString("product_price")%></label>
<input type="hidden" class="form-control" id="product_price" name="product_price" value='<%=rs.getString("product_price")%>'>
</div>
<div class="form-group">
<label for="product_name">Item Quantity: <% out.println(items[i]); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<div class="form-group">
<label for="product_name">Product Total Price: <%
int num1=Integer.parseInt(items[i]);
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<hr>
<% sum= sum + gtotal; %>
<%
}
}
}
%>
<div class="form-group">
<label for="product_name">Grand Total: <%
out.println(sum); %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>
<button type="print" onclick="window.print();" class="btn btn-warning">Print</button>
</form>
</div>
</div>
<%# include file="footer.jsp" %>
</html>
I think the problem in your code is with <input type="text" name="item_no" /> because every row in your above code has checkbox , when you submit your form only selected checkbox is passed , but all <input type="text" name="item_no" /> is passed even the null where you didn't give any value , so when you iterate you get null value with that as well .Now ,to solve this do like below :
//give value to your check-box i.e id of that row
<input type="checkbox" name="products" value ='<%=rs.getString("id")%>' />
//pass that id with your input type i.e item_no_1 ..etc
<input type="text" name='item_no_<%=rs.getString("id")%>' />
And then to get only particular value of Product Quantity where you have selected Checkbox do like below :
String products[] = request.getParameterValues("products");
if (products!= null && items!= null && products.length != 0 && items.length != 0) {
for (int i = 0; i < products.length; i++) {
//your code
<div class="form-group">
<label for="product_name">Product Total Price: <%
//getting item value of that selected checkbox
int item_no=Integer.parseInt(request.getParameter("item_no_" + products[i]));
int propri=Integer.parseInt(rs.getString("product_price"));
out.println(num1 * propri); int gtotal= num1 * propri; %></label>
<input type="hidden" class="form-control" id="item_no" name="item_no" value='items[j]'>
</div>

Thymeleaf: How to retrieve elements from list or database without iterating?

I am trying to check if a field in the database is true or false using the SpEl "th:if="${certs.flag == 'TRUE'}" ". If it is set to "true" the form should not be displayed. I have searched all over but can not seem to find any article that has an example of what i need exactly. Everyone seems to be using iteration. But iteration is not applicable to what i am doing.
#RequestMapping(value = "/cert_prog", method = RequestMethod.GET)
public String examsList(Model model, CertificateProgramme certificateProgramme){
Iterable<Exams> exams = examService.findAll();
Iterable<School> schools = schoolService.findAll();
Iterable<CertificateProgramme> certificateProgrammes = certificateService.findAll();
Iterable<Picture> pictures = pictureService.findAll();
CertificateProgramme mycert = certificateService.flagger(certificateProgramme);
model.addAttribute("mycert", mycert);
model.addAttribute("exams", exams);
if(!model.containsAttribute("newExam")){
model.addAttribute("newExam", new Exams());
}
model.addAttribute("certificateProgrammes", certificateProgrammes);
if(!model.containsAttribute("certificate")){
model.addAttribute("certificate",new CertificateProgramme());
}
model.addAttribute("grades", Grade.values());
model.addAttribute("regions", Region.values());
model.addAttribute("schools",schools);
if(!model.containsAttribute("newSchool")){
model.addAttribute("newSchool",new School());
}
model.addAttribute("picture", new Picture());
return "cert_prog";
}
<form th:if="${certs.flag == 'TRUE'}" method="post" th:object="${certificate}" th:action="#{/basic}" class="form-inline inline new-item">
<div th:replace="common/layout :: flash"></div>
<fieldset>
<legend> Personal Information</legend>
<div class="row" th:classappend="${#fields.hasErrors('fullName')}? 'error' : ''" >
<input type="text" class="form-control input-sm" th:field="*{fullName}" placeholder="Full Name example Jane Doe"/>
<div class="error-message" th:if="${#fields.hasErrors('fullName')}" th:errors="*{fullName}"></div>
</div>
<div class="row" th:classappend="${#fields.hasErrors('date')}? 'error' : ''" >
<input type="date" class="form-control input-sm datepicker" th:field="*{date}"
placeholder="Date Of Birth"/>
<div class="error-message" th:if="${#fields.hasErrors('gender')}" th:errors="*{date}"></div>
</div>
<div class="row" th:classappend="${#fields.hasErrors('Nationality')}? 'error' : ''" >
<input type="text" class="form-control input-sm autocomplete" th:field="*{Nationality}"
placeholder="Nationality"/>
<div class="error-message" th:if="${#fields.hasErrors('Nationality')}" th:errors="*{Nationality}"></div>
</div>
<div class="row" th:classappend="${#fields.hasErrors('email')}? 'error' : ''" >
<input type="text" class="form-control input-sm" th:field="*{email}"
placeholder="Email example jane.doe#example.com"/>
<div class="error-message" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
</div>
<div class="row" th:classappend="${#fields.hasErrors('married')}? 'error' : ''" >
<select th:field="*{married}" class="form-control input-lg">
<option value="">[Select Martial Status]</option>
<option value="Single">Single</option>
<option value="Married">Married</option>
</select>
<div class="error-message" th:if="${#fields.hasErrors('married')}" th:errors="*{married}"></div>
</div>
<div class="row">
<input type="text" class="form-control input-sm" th:field="*{guardianTelephoneNumber}"
placeholder="Guardian Telephone Number"/>
</div>
<div th:classappend="${#fields.hasErrors('courseOffered')}? 'error' : ''">
<input type="text" th:field="*{courseOffered}" placeholder="CourseOffered"/>
<div class="error-message" th:if="${#fields.hasErrors('courseOffered')}" th:errors="*{courseOffered}"></div>
</div>
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</fieldset>
</form>
Your question doesn't really make sense to me. If you want to check a single field from the database, then you can't pass a list of items. For example, in your html:
<div th:each="certs : ${certificateProgrammes}">
<form th:if="${certs.flag == 'TRUE'}">
.
.
.
</form>
</div>
It's going through the list of ${certificateProgrammes} and assigning it to ${certs} each time in the loop. If you don't want to loop, then instead of adding Iterable<CertificateProgramme> certificateProgrammes = certificateService.findAll(); to the model, just add a single object of type CertificateProgramme. Then you can do:
model.addAttribute("certs", /* however you get the specific one you want to check */);
and in the html
<!-- no iterating required, because you know which object you're talking about -->
<form th:if="${certs.flag == 'TRUE'}">

Receive different result from 2 empty form inputs

In a .jsp page I have a form (POST method):
<div class="form-group">
<div class="col-md-3">
<input type="text" list="materials" class="form-control" name="material"/>
<datalist id="materials"></datalist>
</div>
<div class="col-md-3 col-sm-3">
<input type="number" class="form-control" name="quantity" >
</div>
</div>
I delete the html5 element required from the input to see what happens if the user submit the form without filling these inputs. In my servlet, I get this:
String material = request.getParameter("material");
String quantity = request.getParameter("quantity");
System.out.println("material="+ material + "-quantity=" + quantity);
and I get:
material=-quantity=null
The fact that I receive an empty string and a null result is it because of the diferrent type in the input?

Want to display Addition result in textbox.? here is my code

addition.jsp
<form action="addition.jsp">
First Number
<input type="text" name="fno">
<br>
<br>Second Number
<input type="text" name="sno">
<br>
<br>Result
<input type="text" value="<%=Integer.parseInt(request.getParameter(" fno "))+Integer.parseInt(request.getParameter("sno ")) %>">
<br>
<br>
<input type="submit" value="ADD">
</form>
I want to display result in thrid textbox named Result on click of submit button.. i tried this code but getting error ..is there something am i missing..kindly help?
request.getParameter(" fno ") is string not a number therefore it will be a wrong format.
<%
String integer = request.getParameter("fno");
String integer1 = request.getParameter("sno");
int x = integer != null ? Integer.parseInt(integer) : 0;
int y = integer1 != null ? Integer.parseInt(integer1) : 0;
int z=x+y;
%>
<input type="text" name="integer" value="<%=z%>"/>
It can be achieved by simple java script as follow:
<html>
<body>
<form action="addition.jsp">
First Number
<input type="text" name="fno" id="fno"/>
<br>
<br>Second Number
<input type="text" name="sno" id="sno"/>
<br>
<br>Result
<input type="text" id="result"/>
<br>
<br>
<input type="button" value="ADD" onClick="setAddition();"/>
</form>
<script>
function setAddition()
{
var fno=document.getElementById("fno").value;
var sno=document.getElementById("sno").value;
document.getElementById("result").value=parseInt(fno)+parseInt(sno);
}
</script>
</body>
</html>

Scrape Html in Android

I need to scrape a url in my android app. The url returns this block of Html code below:
<div id="main">
<div id="header">
<form action="/search_db.php" id="f1" method="GET">
<div style="float:left; width:829px;">
<span style="margin:15px;"><img src="http://mp3skull.com/img/logo.jpg" border="0" alt="mp3skull.com - mp3 downloads" style="vertical-align:middle;" /></span>
<input type="text" name="q" id="sfrm" autocomplete="off" value="feel good inc gorillaz" style="font-size:18px; vertical-align:middle; width:470px;">
<input type="hidden" name="fckh" value="c1935e9a779034dec31fe7117c456eb8">
<input type="submit" id="search_button" value="Search" style="font-size:18px; vertical-align:middle;">
</div>
<div style="float:left; text-align:right;">
</div>
<div style="clear:both;"></div>
</form><script type="text/javascript">document.getElementById('sfrm').focus();InstallAC(document.getElementById('f1'), document.getElementById('sfrm'), document.getElementById('search_button'), '', 'en');</script>
</div>
Kindly show me an example of how to extract the values of the returned html code in java
Using jsoup.
Document doc = Jsoup.connect("http://your/url/here").get(); // or Jsoup.parse(htmlString);
Elements header = doc.select("#header"); //access to <div id="header">...</div>
Elements inputs = header.select("input");
for(Element input : inputs){
System.out.println(input); //print <input>....</input>
System.out.println(input.attr("id")); //printing attribute id
}

Categories