Currently I am practicing on custom form validation using JavaScript. I have made a form which takes product detail input from the user and then through JavaScript I am doing the validation.
when a user selects "product type" physical then three input fields are activated. When the user clicks save button then the validation starts.
The validation works fine until it starts validating the product type and it's related fields.
First the program checks whether the "product type" is physical or downloadable, then if the product is physical then its starts validating product weight, product depth, and product height. It should have continue further with validating the other else if conditions but it just jumps out. I don't know why this is happening.
//function to check input data and save it.
function save_data(){
var product_name_value = product_name.value;
var product_price_value = product_price.value;
var product_category_value = product_category_selector.value;
var product_type_value = product_type_selector.value;
var product_height = document.getElementById('product-height-field');
var product_weight = document.getElementById('product-weight-field');
var product_depth = document.getElementById('product-depth-field');
var product_weight_value = product_weight.value;
var product_height_value = product_height.value;
var product_depth_value = product_depth.value;;
var product_link_value = product_link.value;
var product_brand_value = product_brand.value;
var product_tags_value = product_tags.value;
var product_description_value = product_description.value;
//To check if any value is blank.
if(product_name_value == ""){
alert("Kindly provide product name.");
product_name.focus();
product_name.style.borderColor = "#e54b4b";
}
else if(product_price_value == ""){
alert("Kindly provide product price.");
product_price.focus();
product_price.style.borderColor = "#e54b4b";
}
else if(product_category_value == "Select Category"){
alert("Kindly select a product category.");
product_category_selector.focus();
product_category_selector.style.borderColor = "#e54b4b";
}
else if(product_type_value == "Select Type"){
alert("Kindly select product type.");
product_type_selector.focus();
product_type_selector.style.borderColor = "#e54b4b";
}
else if(product_type_value == "Downloadable"){
if(product_link_value == ""){
alert("Kindly provide download link of the product.");
product_link.focus();
product_link.style.borderColor = "#e54b4b";
}
}
else if(product_type_value == "Physical"){
if(product_weight_value == ""){
alert("Kindly provide product weight.");
product_weight.focus();
product_weight.style.borderColor = "#e54b4b";
}
else if(product_height_value == ""){
alert("Kindly provide product height.");
product_height.focus();
product_height.style.borderColor = "#e54b4b";
}
else if(product_depth_value == ""){
alert("Kindly provide product depth");
product_depth.focus();
product_depth.style.borderColor = "#e54b4b";
}
else{
}
}
else if(product_brand_value == ""){
alert("Kindly provide product brand.");
product_brand.focus();
product_brand.style.borderColor = "#e54b4b";
}
else if(product_tags_value == ""){
alert("Kindly provide product tags.");
product_tags.focus();
product_tags.style.borderColor = "#e54b4b";
}
else if(product_description_value == "" || product_description_value.length < 150){
alert("The product cannot be left blank and cannot be less than 150 characters.");
product_description.focus();
product_description.style.borderColor = "#e54b4b";
}
else{
alert("Product Details save successfully.");
}
}
<div id="primary-details" class="details-blocks">
<table>
<tbody>
<tr>
<td class="product-details">Product Name* : </td>
<td class="product-detail-input"><input type="text" name="product-name" id="product-name-field"></td>
</tr>
<tr>
<td class="product-details">Product Price* : </td>
<td class="product-detail-input"><input type="number" name="product-price" id="product-price-field"></td>
</tr>
<tr>
<td class="product-details">Product Category* : </td>
<td class="product-detail-input">
<select id="product-category">
<option selected hidden>Select Category</option>
</select>
</td>
</tr>
<tr>
<td class="product-details">Product Type* : </td>
<td class="product-detail-input">
<select id="product-type">
<option selected hidden>Select Type</option>
<option name = "product type" value="Physical">Physical</option>
<option name = "product type" value="Downloadable">Downloadable</option>
</select>
</td>
</tr>
<tr id="product-weight">
<td class="product-details">Product Weight* : </td>
<td class="product-detail-input"><input type="text" name="product-weight" id="product-weight-field"></td>
</tr>
<tr id="product-height">
<td class="product-details">Product Height* : </td>
<td class="product-detail-input"><input type="text" name="product-height" id="product-height-field"></td>
</tr>
<tr id="product-depth">
<td class="product-details">Product Depth* : </td>
<td class="product-detail-input"><input type="text" name="product-depth" id="product-depth-field"></td>
</tr>
<tr>
<td class="product-details">Product Brand* : </td>
<td class="product-detail-input"><input type="text" name="product-brand" id="product-brand-field"></td>
</tr>
<tr id="product-download">
<td class="product-details">Product Download Link* : </td>
<td class="product-detail-input"><input type="text" name="product-download-link" id="product-brand-field"></td>
</tr>
<tr>
<td class="product-details">Product Tags* : </td>
<td class="product-detail-input"><input type="text" name="product-tags" id="product-tags-field"></td>
</tr>
</tbody>
</table>
</div>
<!-- primary detail section -->
<!-- description block -->
<div id="description-block">
<p>Product Description*:</p>
<textarea id="description-textfield"></textarea>
</div>
<!-- description block -->
<button id="save-button" class="control-buttons">Save</button>