validation - JQuery check if select fields have a certain value and add required attribute -


ok so, add attribute required 'please specify' text box if value of dropdown/select includes 'please specify'.

i can make field validate single value using code below can't seem check options. need check if of selected options have 'please specify' , if so, applies required attribute. i've tried million different ways , tried using array couldn't figure it. sure it's super easy amazing. big in advance!

html: select field

<select name="billing_customer_info" id="billing_customer_info" class="select " data-placeholder="please select option" allow_null="0" nulllabel="please select option" presentation="select" display="" hasselected="false">   <option value="internet search please specify">internet search please specify</option>   <option value="friend told me">friend told me</option><option value="advertisement please specify">advertisement please specify</option>   <option value="sticker">sticker</option>   <option value="newspaper or magazine please specify">newspaper or magazine please specify</option>   <option value="social media please specify">social media please specify</option>   <option value="festival or show please specify">festival or show please specify</option>   <option value="in shop">in shop</option> </select> 

my field needs have attribute added if select has please specify

<input type="text" class="input-text " name="billing_please_specify_here" id="billing_please_specify_here" placeholder="enter more information" display="text"> 

jquery:

$('select[name=billing_customer_info]').change(function () {     if ($('select[name=billing_customer_info]').val() == 'internet search please specify'){         $('input[name=billing_please_specify_here]').attr("required", true);     } else {         alert ('i dont need mandatory');         $('input[name=billing_please_specify_here]').attr("required", false);     }     }); 

use indexof function check selected value contains please specify or not. add required attribute use attr("required", "required") , remove use removeattr("required"). can following.

$('select[name=billing_customer_info]').change(function () {     if ($(this).val().tolowercase().indexof('please specify')>-1) {         $('input[name=billing_please_specify_here]').attr("required", "required");     } else {         alert('i dont need mandatory');         $('input[name=billing_please_specify_here]').removeattr("required");     } }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -