View Full Version : Resolved Now to determine if select option is empty

02-07-2011, 10:45 AM
I'm trying to validate a form select option. If select is ignored, an alert should be displayed. Select id is 'status'.

if($("select#status option").val()===''){alert("Make a selection in the Status field");return false;}
I tested this and it failed.



02-07-2011, 12:23 PM

Either of these will give you the selected value:

$('#status option:selected').val();

What does 'failed' mean?

02-07-2011, 03:40 PM
I post the related form without selecting any option but it submits anyway.

02-07-2011, 05:17 PM
What event are you firing your validation code on? Do you receive any error messages? Can you post more of your code?

02-07-2011, 06:34 PM
I listed the select#status three times in the jquery, just trying to find one that would pass info. Seems like the variable is just never created so there is no value to hook on to. The text fields work fine. I am not able to determine if the select options are being ignored.

The form:

<form method="post" action="csmemberchangepost.php" style='background:#cccccc'>
<select name="m_id">
<?php showmem();?>(This lists the members in a drop down list. Provides the member id)
<p style='font:16px Arial'><label for "name">Name</label><br><input type="text" size="60" name="name" style='font:16px Arial'></p>
<p style='font:16px Arial'><label for status>Status</label><br>
<select size="3" name="status" id="status">
<option value='Verified' style='font:16px Arial;width:150px;text-align:center'>Verified</option>
<option value='NonVer' style='font:16px Arial;width:150px;text-align:center'>NonVer</option>
<option value='New' style='font:16px Arial;width:150px;text-align:center'>New</option>
<p style='font:16px Arial'><label for "special">Special Instructions</label><input type='text' size='60' name='special' style='font:16px Arial'></p>
<p style='text-align:center;margin-top:1em;'><input type="submit" id="submit" value="Update File" style='font: bold 20px Arial'></p>

The jquery:

<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
if($("input[name='name']").val() === ''){alert("Name must be entered.");return false;}
if($("input[name='special']").val()===''){alert("Special entry must be made");return false;}
if($("select#status option").val()===''){alert("Make a selection in the Status field");return false;}
if($("#status option:selected").val()==='null'){alert("Make a option:selected in the Status field");return false;}
if($("select#status option").val()==='null'){alert("Make a status option in the Status field");return false;}

02-07-2011, 07:19 PM
Ok, first, some pointers:

Give elements ID's to make them easier to access with JS and CSS
Don't use reserved words, like "name" for naming things
Double quotes are preferable for HTML attributes. Either way, choose one and stick to it.
Inline styles are hard to maintain. Put styles in the <head> and apply them with CSS selectors
Attach form validation code to the form's "submit" handler
ALWAYS VALIDATE YOUR CODE (http://validator.w3.org/)

With those in mind, here's a reworked version:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>

<style type="text/css">
#f1 p, #f1 input{
font:16px Arial;
#f1 input[type=submit]{
font: bold 20px Arial;
#f1 label{
#f1 option{
font:16px Arial;

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if($("#txtName").val() === ''){alert("Name must be entered.");return false;}
if($("#txtSpecial").val() === ''){alert("Special entry must be made");return false;}
if($("#selStatus").val()=== null){alert("Make a selection in the Status field");return false;}
return false;




<form id="f1" method="post" action="csmemberchangepost.php">
<label for="txtName">Name</label>
<input type="text" size="60" id="txtName" name="txtName"></p>
<label for="selStatus">Status</label>
<select size="3" name="selStatus" id="selStatus">
<option value="Verified">Verified</option>
<option value="NonVer">NonVer</option>
<option value="New">New</option>
<label for="txtSpecial">Special Instructions</label>
<input type="text" size="60" name="txtSpecial" id="txtSpecial">

<div class="submitwrap"><input type="submit" id="submit" value="Update File"></div>



02-07-2011, 07:40 PM
Works perfectly and I can now understand why.
I have printed this post for reference.
Thank you on all points.