I have a jsp file contains a form and I need to validate the form. The jsp page will be opened as pop-up page and hence I choose not to pop-up the validation message again. Here is my code.
<script type="text/javascript">
function validateForm() {
var x=document.forms["myForm"]["name1"].value;
if (x.trim().length<=4){
return false;
}
}
</script>
<form name="myForm" method="POST" action="write.jsp" onsubmit="return validateForm();">
Name: <input type="text" name="name1" value="<%=name%>" size="20"/> <br>
Price: <input type="text" name="price" value="<%=price%>" size="20" /><br>
Due time: <input type="text" name="DueTime" value="<%=dueday%>" size="20" /><br>
Location: <input type="text" name="Location" value="<%=address%>" size="20" /><br>
Photo Url: <input type="text" name="url" value="<%=imagePath%>" size="20" /><br>
Description: <input type="text" name="desc" value="<%=desc%>" size="20" /><br>
<input type="submit" name="submit" onclick="window.close()"/>
<% if(validateForm = false) { %> <a>error message</a> <% } %>
</form>
I am validating for first input "name1" only and the input length is less than 4.
I'd like to display a error message below the submit button. The codes is not working, can anyone help me to fix this?
document.getElementsByName('name1')[0].value;
in place of
document.forms["myForm"]["name1"].value;
use a span below the submit button
html
<span id="error"></span>
script
function validateForm()
{
var x=document.getElementsByName('name1')[0].value;
if (x.trim().length<=4)
{
document.getElementById("error").innerHTML="atleast 4 letters required in name";
return false;
}
}
Use document.getElementsByName("name1") instead of document.forms["myForm"]["name1"].value;
Use as
function validateForm()
{
var x=document.getElementsByName("name1").value;
if (x.trim().length<=4)
{
alert("Message");
return false;
}
}
Related
I'm trying to upload a file to cloudinary. I'm stucked at how to only get parts of image from the form. It keeps on throwing exception: Invalid image file. If I remove all text inputs in the form, the uploading is successful. I guess that happens because the form also has text inside. Please help me solve this. I'm really grateful for your support.
Here is my code:
Form.jsp:
<form role="form" action="<c:url value="/admin/product/update"/>" method="post" enctype="multipart/form-data">
<input name="id" value="${product.id}" hidden="">
<div class="form-group">
<label>Name:</label> <input class="form-control" value="${product.name}" name="name" />
</div>
<div class="form-group">
<label>Price:</label> <input class="form-control" value="${product.price}" type="number" name="price" />
</div>
<div class="form-group">
<label>Quantity:</label> <input class="form-control" value="${product.quantity}" type="number" name="quantity" />
</div>
<div class="form-group">
<label>Image:</label> <input class="form-control" value="${product.image}" name="image" />
</div>
<div class="form-group">
<label>Description </label> <br>
<textarea rows="4" cols="50" name="description" value="${product.description}" ></textarea>
</div>
<div class="form-group">
<label>Category</label>
<div class="checkbox">
<select name="catid">
<c:forEach items="${categorylist}" var="c">
<option value="${c.id}">${c.name}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group">
<label>image</label> <input type="file" name="image" value="${product.image }" />
</div>
Servlet.java
BeanUtils.populate(product, request.getParameterMap());
//if (catid != product.getCategory().getId()) {
// Category category = new Category();
category = dao2.getCategoryByID(catid);
product.setCategory(category);
Map result = null;
Collection<Part> fileParts = request.getParts();
for (Part part : fileParts) {
String fileName = part.getSubmittedFileName();
result = UploadImage.uploadImage(fileName, part);
String url = String.valueOf(result.get("url"));
product.setImage(url);
if (result == null) {
throw new RuntimeException("Loi upload");
}
}
dao.update(product);
The Cloudinary upload method supports uploading media files from the sources like a local path, a remote URL, a private storage URL (S3 or Google Cloud storage), a base64 data URI, or an FTP URL.
Based on your code, it seems that you are only supplying the filename of the image.
String fileName = part.getSubmittedFileName();
result = UploadImage.uploadImage(fileName, part);
You would need to update the code to input the local path of the image.
Code to post PayPal form.
<body>
<form accept-charset="utf-8"
action="https://www.sandbox.paypal.com/au/cgi-bin/webscr" id="frm"
method="post">
<input type="hidden" name="business"
value="aaaa-facilitator#gmail.com"> <input
type="hidden" name="cancel_return"
value="http://localhost:8080/abc-jaxrs/web/cancel_redirection"> <input type="hidden"
name="cmd" value="_xclick"> <input type="hidden"
name="charset" value="utf-8"> <input type="hidden"
name="currency_code" value="USD"> <input type="hidden"
name="notify_url" value="http://localhost:8080/abc-jaxrs/web/success_redirection"> <input
type="hidden" name="return" value="http://localhost:8080/abc-jaxrs/web/success_redirection"> <input
type="hidden" name="item_name" value="123"> <input
type="hidden" name="amount" value="0.1">
</form>
<p style="text-align: center">
<h4>Redirecting To PayPal...</h4>
<script>
$(this.document).ready(function() {
$('.loaderparent').show();
var frm = $("form");
frm.submit();
});
</script>
Code to retrieve data from data from Java controller.
#RequestMapping(value = "/success_redirection", method = RequestMethod.GET)
public ModelAndView successRedirection(HttpServletRequest request, HttpServletResponse response) {
String itemName = (String) request.getAttribute("item_name"); // or
String itemNname = request.getParameter("item_name");
}
But getting itemName value as null in both cases.
So how to retrieve success/failure data from PayPal in JAVA.
Ensure enabling the Instant Payment Notification (IPN) from paypal account setting along with the success URL.
My Profile > My Selling Tools> Instant payment notifications >update then like below.
I made a web project from jsp and hibernate, on my from have a checkbox... I got no problem when insert the data but when update the data I got this [Ljava.lang.String in every first checkbox on my form and the value of my first checkbox doesn't store to database but the other checkbox work well
one more thing how to check the checkbox value from database??
this part of my jsp
<td><spring:message code="authorityRight" text="default text" /></td>
<td>:</td>
<td><fieldset id="menu_e">
<input type="checkbox" name="menu" value="User" class="ceksmenu">User<br />
<fieldset id="sub_menu_user_e">
<input type="checkbox" name="sub_menu_user" value="User1" class="ceksmuser">User1
<input type="checkbox" name="sub_menu_user" value="User2" class="ceksmuser">User2
<input type="checkbox" name="sub_menu_user" value="User3" class="ceksmuser">User3<br />
</fieldset>
<input type="checkbox" name="menu" value="Monitoring" class="ceksmenu">Monitoring<br />
<fieldset id="sub_menu_monitoring_e">
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring1" class="ceksmmonit">Monitoring1
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring2" class="ceksmmonit">Monitoring2
<input type="checkbox" name="sub_menu_monitoring" value="Monitoring3" class="ceksmmonit">Monitoring3<br />
</fieldset>
<input type="checkbox" name="menu" value="Parameter" class="ceksmenu">Parameter<br />
<fieldset id="sub_menu_parameter_e">
<input type="checkbox" name="sub_menu_parameter" value="Parameter1" class="ceksmparam">Parameter1
<input type="checkbox" name="sub_menu_parameter" value="Parameter2" class="ceksmparam">Parameter2
<input type="checkbox" name="sub_menu_parameter" value="Parameter3" class="ceksmparam">Parameter3</fieldset>
</fieldset>
</td>
this is my javascript
<script type="text/javascript">
$(document).ready(function() {
$('.update').click(function() {
var id_update = $(this).attr("id_update");
var nama_authority = $(this).attr("nama_authority");
var menu = $('input:checkbox[name=menu]').is(':checked');
var sub_menu_user = $('input:checkbox[name=sub_menu_user]').is(':checked');
var sub_menu_monitoring = $('input:checkbox[name=sub_menu_monitoring]').is(':checked');
var sub_menu_parameter = $('input:checkbox[name=sub_menu_parameter]').is(':checked');
$('#id_authority_e').val(id_update);
$('#id_authority_l').val(id_update);
$('#nama_authority_e').val(nama_authority);
$('#menu_e').val(menu);
$('#sub_menu_user_e').val(sub_menu_user);
$('#sub_menu_monitoring_e').val(sub_menu_monitoring);
$('#sub_menu_parameter_e').val(sub_menu_parameter);
});
$('.delete').click(function() {
var id_delete = $(this).attr("id_delete");
$('#id_authority_d').val(id_delete);
});
$('#example').dataTable({
});
});
</script>
and this is my controller
public class Operator {
#RequestMapping(value = "/admin/updateAuthority.html", method = RequestMethod.POST)
public ModelAndView updateAuthorityUser(ModelMap model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String id_Authority = request.getParameter("id_authority");
AuthorityUser authorityuser = authorityuserService.get(id_Authority);
String nama = request.getParameter("nama_authority");
String[] menu = request.getParameterValues("menu");
String[] sb_user = request.getParameterValues("sub_menu_user");
String[] sb_monitor = request.getParameterValues("sub_menu_monitoring");
String[] sb_parameter = request.getParameterValues("sub_menu_parameter");
authorityuser.setId_authority(id_Authority);
authorityuser.setNama_authority(nama);
authorityuser.setMenu(menu);
authorityuser.setSub_menu_user(sb_user);
authorityuser.setSub_menu_monitoring(sb_monitor);
authorityuser.setSub_menu_parameter(sb_parameter);
authorityuserService.update(authorityuser);
model.addAttribute("successUpdate", "true");
return listAuthorityUser(model);
}
}
any help will be pleasure :)
When doing upload the file from jsp page. The request.getContentType() and String type1 = req.getHeader("Content-Type") are return the null value.
Jsp Code:
<FORM action="/UploadServlet" method="post" enctype="multipart/form-data">
<INPUT type="file" name="name_of_files">
<INPUT type="submit" name="Submit" value="Submit">
</FORM>
I am using html5 validation for my form in jsp page. I have given novalidate in form. When i click on submit button of that form it should validate the form now and display all validation in one label which was hidden before.How can i get these in jsp?
This is my form code.
<form action="#join1_form" method="POST">
<div>
<input type="text" name="firstname" id="firstname" value="" required placeholder="First Name" pattern="\w+" />
<input type="text" name="lastname" id="lastname" value="" required placeholder="Last Name" pattern="\w+" />
</div>
<div>
<input type="email" name="email" placeholder="Your Email" id="email" value="" required />
</div>
<div>
<input name="password" id="password" type="password" required placeholder="New Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}" name="password" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Password must contain at least 6 characters, including UPPER/lowercase and numbers' : '');
if(this.checkValidity()) form.cpassword.pattern = this.value;">
</div>
<div>
<input name="cpassword" id="cpassword" type="password" placeholder="Re-enter Password" required pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}" name="cpassword" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');">
</div>
<div>
<input type="label" hidden value="">
</div>
<input type="submit" value="Next">
</form>
I want to display all the validation notes after submitting the submit button.These validations should displaye in textfield which is given hidden here.how can i do that?
this is my code for not repeating the emailID. where should i put this in above form?How to display its validation in the textfield which is given hidden above?
<%
Connection conn = null;
PreparedStatement ps=null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "");
String email1=request.getParameter("lemail");
try{
String q="SELECT * FROM userdetails where email=?";
ps=conn.prepareStatement(q);
ps.setString(1,email1);
rs=ps.executeQuery();
if(rs.next())
{
//what to do here?
}
else
{
//what to do here?
}
}
catch(Exception e)
{
e.printStackTrace();
}
try{
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
if(conn!=null){
conn.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<html:form action="storenotes" onsubmit="return demo()">
call demo() function in your JavaScript
and submit form by
return true
or
return false