Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2011, 09:45 AM   PM User | #1
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
Now to determine if select option is empty

I'm trying to validate a form select option. If select is ignored, an alert should be displayed. Select id is 'status'.
Code:
if($("select#status option").val()===''){alert("Make a selection in the Status field");return false;}
I tested this and it failed.

Thanks,

Andy

Last edited by 1andyw; 02-07-2011 at 06:41 PM.. Reason: Resolved
1andyw is offline   Reply With Quote
Old 02-07-2011, 11:23 AM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
http://api.jquery.com/val/

Either of these will give you the selected value:

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

What does 'failed' mean?
Spudhead is offline   Reply With Quote
Old 02-07-2011, 02:40 PM   PM User | #3
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
I post the related form without selecting any option but it submits anyway.
1andyw is offline   Reply With Quote
Old 02-07-2011, 04:17 PM   PM User | #4
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
What event are you firing your validation code on? Do you receive any error messages? Can you post more of your code?
Spudhead is offline   Reply With Quote
Old 02-07-2011, 05:34 PM   PM User | #5
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
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:
PHP Code:
<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)
</select>
<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>
</select></p>
<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>
</form>
The jquery:

Code:
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#submit").click(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;}
});
});
</script>
1andyw is offline   Reply With Quote
Old 02-07-2011, 06:19 PM   PM User | #6
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Ok, first, some pointers:
  1. Give elements ID's to make them easier to access with JS and CSS
  2. Don't use reserved words, like "name" for naming things
  3. Double quotes are preferable for HTML attributes. Either way, choose one and stick to it.
  4. Inline styles are hard to maintain. Put styles in the <head> and apply them with CSS selectors
  5. Attach form validation code to the form's "submit" handler
  6. ALWAYS VALIDATE YOUR CODE

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

PHP Code:
<!DOCTYPE HTML>
<
html>
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<
title>Untitled Document</title>

<
style type="text/css">
#f1{
    
background-color:#cccccc;
}
#f1 p, #f1 input{
    
font:16px Arial;
}
#f1 input[type=submit]{
    
fontbold 20px Arial;
}
#f1 label{
    
display:block;    
}
#f1 option{
    
font:16px Arial;
    
width:150px;
    
text-align:center
}
.
submitwrap{
    
margin-top:1em;
    
text-align:center;
}
</
style>

<
script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#f1").submit(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;
    });
});

</script>

</head>

<body>

<form id="f1" method="post" action="csmemberchangepost.php">
<p>
    <label for="txtName">Name</label>
    <input type="text" size="60" id="txtName" name="txtName"></p>
<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>
    </select>
</p>
<p>
    <label for="txtSpecial">Special Instructions</label>
    <input type="text" size="60" name="txtSpecial" id="txtSpecial">
</p>

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

</form>

</body>
</html> 
Spudhead is offline   Reply With Quote
Users who have thanked Spudhead for this post:
1andyw (02-07-2011)
Old 02-07-2011, 06:40 PM   PM User | #7
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
Works perfectly and I can now understand why.
I have printed this post for reference.
Thank you on all points.

Andy
1andyw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:14 AM.


Advertisement
Log in to turn off these ads.