如何使用javascript验证下拉列表? [英] how to validate a dropdownlist using javascript?

查看:76
本文介绍了如何使用javascript验证下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想如何在asp.net中使用javascript验证下拉列表?

How i want to validate a dropdownlist using javascript in asp.net???

推荐答案





使用以下JS脚本



Hi,

Use below JS script

function JSFunctionValidate()
{
if(document.getElementById('<%=ddlView.ClientID%>').selectedIndex == 0)
{
alert("Please select ddl");
return false;
}
return true;
}





如果你想查看价值然后写一下



if you want to check with value then write

objDDl = document.getElementById('<%=ddlView.ClientID%>');
if(objDDl.options[objDDl.selectedIndex].value == "")
{
alert("Please select ddl");
return false;
}


这有点容易。看下面这个函数



it's bit easy. see the following function

function validate()
{
   if(document.getElementById("ddlList").value == "")
   {
      alert("Please select value"); // prompt user
      document.getElementById("ddlList").focus(); //set focus back to control
      return false;
   }
}


假设你的select元素看起来像这样

Assuming your select element looks like this
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>




function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;
//if you need text to be compared then use
var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0) //for text use if(strUser1=="Select")
{
alert("Please select a user");
}
}


这篇关于如何使用javascript验证下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆