'Cannot check and validate value from form
I'm making a simple web application with Java Servlet and JSP. In the JSP i have a form like the code below and a js function to check the input before submitting to the Servlet "insert". In case of completing all inputs it seems that there are no problems. But when I leave all the inputs empty, normally, the function should check that all of them are empty, the return false and do not submit the data. However, the form is still submitted and I don't know what's wrong with my code. Additionally, I have tried the function checkSubmit() only return false and it worked(not submit the form). Moreover, if I declare only variable name without adding any more variables(dob, id, ...) and check only if name === "" it still worked. The real problem happened when i declare more variables. Can anyone help me? Thanks very much. Have a nice day!
function checkSubmit() {
var name = document.getElementById('name').value.trim();
var dob = document.getElementById('dob').value.trim();
var id = document.getElementById('id').value.trim();
var phone = document.getElementById('phone').value.trim();
var address = document.getElementById('address').value.trim();
if(name === "" || dob === "" || id === "" || phone === "" || address === ""){
alert('Recheck the input');
return false;
} else {
return true;
}
}
<form action="insert" method="POST" onsubmit="return checkSubmit();">
<div class="input_field">
<div class="personal_info name">
<label for="name">Họ và tên: </label>
<input type="text" id="name" name="name" placeholder="Nguyễn Văn A ...">
</div>
<div class="personal_info birthday">
<label for="birthday">Ngày tháng năm sinh: </label>
<input type="date" id="birthday" name="dob">
</div>
<div class="gender">
<label for="gender">Giới tính: </label>
<select name="gender" id="gender">
<option value="male">Nam</option>
<option value="female">Nữ</option>
</select>
</div>
<div class="personal_info identification">
<label for="identification">Số chứng minh thư ( CCCD nếu có <span style="color: red;">*</span> ): </label>
<input type="text" name="id" id="identification" placeholder="VD: 034202000167">
</div>
<div class="personal_info phone">
<label for="phone">Số điện thoại: </label>
<input type="text" id="phone"
name="phone" placeholder="0123-456-789">
</div>
<div class="personal_info address">
<label for="address">Địa chỉ thường trú ( Quê quán ) : </label>
<input type="text" id="address" placeholder="Số nhà, ngõ, đường ..." name="address">
</div>
</div>
<p style="text-transform: uppercase; padding: 20px 20px 0; font-weight: bold; font-size: 25px;"><i class="fa fa-info-circle"></i> Thông tin phòng thuê: </p>
<div class="default_field">
<input type="hidden" name="rid" value="${requestScope.roomByID.rid}">
<p><span>Mã phòng</span>: ${requestScope.roomByID.rid}</p>
<p><span>Loại phòng</span>: phòng ${requestScope.roomByID.type} người</p>
<p><span>Tiền phòng/tháng</span>: ${requestScope.roomByID.price} VNĐ</p>
</div>
<button type="submit" id="send">xác nhận</button>
</form>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
