toolman Posted March 2, 2016 Share Posted March 2, 2016 Hi there,I have the following line which insets an option to the top of a select. What I would like to do is to check if this value is left as the default and return an alert or a validation message asking the user to select an option apart from the default. $("select[name='ap']").prepend("<option value='' selected='selected'>Default option</option>"); What would be the best way to do this?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/ Share on other sites More sharing options...
fastsol Posted March 2, 2016 Share Posted March 2, 2016 Something like this, untested. Best thing to do is asign a valid id to the select box so you can target it much easier. In my example I simply called it "ap". $("#ap").prepend("<option value='' selected='selected'>Default option</option>"); if($('#ap option:selected').text() == "Default option") { alert('Please choose a valid option'); } Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/#findComment-1531636 Share on other sites More sharing options...
Psycho Posted March 2, 2016 Share Posted March 2, 2016 Assuming that this default value is the only one with an empty value, I would suggest checking the value of the field instead of the text/label. Labels can and do change (e.g. showing different labels based on the user's preferred language), but the values should not change. Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/#findComment-1531637 Share on other sites More sharing options...
fastsol Posted March 2, 2016 Share Posted March 2, 2016 Take hints from what Psycho said above. I also thought you may need to be able to check the value on the fly as people switch between options. $("#ap").change(function(){ if($('#ap option:selected').text() == "Default option") { alert('Please choose a valid option'); } }); Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/#findComment-1531638 Share on other sites More sharing options...
toolman Posted March 3, 2016 Author Share Posted March 3, 2016 Take hints from what Psycho said above. I also thought you may need to be able to check the value on the fly as people switch between options. $("#ap").change(function(){ if($('#ap option:selected').text() == "Default option") { alert('Please choose a valid option'); } }); Thanks this works when the user chooses another option and then selects the default, but is there a way to create the alert if the default value is left when submitting the form? Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/#findComment-1531650 Share on other sites More sharing options...
maxxd Posted March 3, 2016 Share Posted March 3, 2016 You can check the value of the field on submission with something like the following completely untested code: $('#ap').prepend("<option value='-1'>This is the default option</option>"); $('form').submit(function(e){ e.preventDefault(); if($('#ap').val() == -1){ alert('Fill in the field, please.'); return false; } ... // continue processing } Of course, you'll want to check the value again during the server-side processing before you do anything with the value, like the also completely untested code below: if($_POST['ap'] == -1){ throw new Exception('Fill in the field, please.'); } ... // continue processing Quote Link to comment https://forums.phpfreaks.com/topic/300918-validate-if-select-has-a-specified-option/#findComment-1531654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.