Hi,
I cant check combo value below. Where is the mistake?
Thanks

<script Language="JavaScript">
function validateR(){
var selectedCombobox=(form2.selectCombo.value);
if (selectedCombobox=='All') {
alert("Please select something");
return false;
}
return true;
}
</script>
<select name="selectCombo">
        <option value="All">All</option>
        <option value="Other">Other</option>
  </select>

This basic example will force the user to select atleast 1 option from the selection list. Hope you had a great time! Enjoy coding...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SomeTitle</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!-- BEGIN HIDING
function validateR(form)
<!-- BEGIN HIDING
 //You may try this 1! Enjoy coding -->
{
do
{ 
if ( form.selectCombo.options[0].selected ) { alert('\nKey-in ( 1 ) For Option 1\nKey-in ( 2 ) For Option 2' ); var ask = prompt('Please select atleast 1 option!');  
form.selectCombo.selectedIndex = ask; } 
if (!form.selectCombo.options[0].selected) { alert('\nDo the next thing!'); } 
} while( form.selectCombo.options[0].selected );
} 
// DONE HIDING -->
</script>
</head>
<body>
<form name="yourForm"  action="#" onsubmit="return false;">
<select name="selectCombo" onblur="validateR(this.form)"> 
<option value="All">All</option> 
<option value="Other">Other</option>
<option value="Other">Another Option</option> 
</select>
</form>
</body>
</html>

Just to make it more precise!

<script type="text/javascript">
<!-- BEGIN HIDING

var nval = /^[1-2]{1}?$/;

function validateR(form)
<!-- BEGIN HIDING
 //To make it more precise we wil addUp some regExp -->
{
do
{ 
if ( form.selectCombo.options[0].selected ) { alert('\nKey-in ( 1 ) For Option 1\nKey-in ( 2 ) For Option 2' ); ask = prompt('Please select atleast 1 option!');  
form.selectCombo.selectedIndex = ask; }
 
//Additional tidBits! -->
if ( !nval.test( ask )) { alert('\nOnly numbers from 1 and 2 is allowed!'); }
if (!form.selectCombo.options[0].selected) { alert('\nDo the next thing!'); } 
} while( form.selectCombo.options[0].selected );
}
 
// DONE HIDING -->
</script>

Solved. Thanks Essential

hii friends!! i need to keep validation to my combobox where it should allow the selection of only available values in combobox....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.