Skip to content Skip to sidebar Skip to footer

How To Validate Select Through Javascript

I want to validate select option with javascript like if user select Admin then this page work for admin Login, Now if user select vendor then for vendor while user for userlogin

Solution 1:

Maybe something like this (without JS)?

<selectname="userType"><optionvalue="Admin">Admin</option><optionvalue="Vendor">Vendor</option><optionvalue="User">User</option></select><?php// phpif ('Admin' == $_POST['userType']) {
  // work as admin
} elseif ('Vendor' == $_POST['userType']) {
  // work as vendor
} else {
  // work as user or any other option
}
?>

Solution 2:

in javascript it will be something like this

var selectObj = document.getElementById("selObj");
   var selectedValue = selectObj.options[selectObj.selectedIndex].value;
   if (selectedValue  == "Admin")

   else

Assign id to select

Solution 3:

I'd use a lib like jQuery just to make it easier but essentially with jQuery to get the selection then pass it to php I would use the .change(), .post()

http://api.jquery.com/change/ http://api.jquery.com/jQuery.post/ http://api.jquery.com/selected-selector/

You would use the change event to see what the selected value was on the select, then the post to pass the data to PHP. If i wasn't so tired right now I would further this point with example code. But I'm to fried for that right now. After that you would use something like

window.location = page2go2; //based on value of selected to load whatever page per.

Solution 4:

USING JS

First, you have to put an id for your dropdownlist and add an eventhandler.

<selectid="myId"onchange='Validate()';><option>Admin</option><option>Vendor</option><option>User</option></select>

Then, access that dropdownlist using javascript via it's id.

<SCRIPTLANGUAGE="javascript"><!--
 function Validate()
{
  var dll=document.getElementById("myId")
   var myindex  = dll.selectedIndex
   var SelValue = dll.options[myindex].value

  //do something with selected value
  }
  //--></SCRIPT>

Or you could use an eventhandler that is invoked when a button is clicked to validate the dropdrownlist.

Use this Code Project article or this one as your guide. Cheers!

Post a Comment for "How To Validate Select Through Javascript"