文本框中的javascript验证 [英] javascript validation in textbox
本文介绍了文本框中的javascript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,其中有一个下拉列表和一个文本框。此文本框取决于下拉选择。当下拉值为其他时,则启用文本框,否则将禁用该文本框。我想在文本框中应用javascript验证,当启用文本框时,在提交表单时它不应该是空的。
function sugvalidate(){
var related = document .getElementById(< span class =code-string>' <%= DropDownList1.ClientID%>')。value;
var cat = document .getElementById(' <%= txt_category.ClientID%>')。value;
if (相关== 其他 ){
if (cat == ){
alert( 请指定类别 );
return false ;
}
}
}
< asp:DropDownList ID = DropDownList1 runat = server CssClass = ddcss AutoPostBack = < span class =code-keyword> True onselectedindexchanged = DropDownList1_SelectedIn dexChanged >
< asp:ListItem 文字 = 错误/错误/问题/问题 值 = 9 > < / asp:ListItem >
< asp:ListItem 文字 = 其他 值 = 10 > < / asp:ListItem >
< / asp:DropDownList >
< asp:TextBox ID = txt_category runat = server 已启用 = False ReadOnly = True > < / asp:TextBox >
< asp:按钮 ID = btn_submit < span class =code-attribute> runat = server 文本 = 提交 OnClientClick = return sugvalidate(); onclick = btn_submit_Click / >
protected void DropDownList1_SelectedIndexChanged( object sender,EventArgs e)
{
if (DropDownList1.SelectedItem.Text == < span class =code-string>其他)
{
txt_category.ReadOnly = false ;
txt_category.Enabled = true ;
}
其他
{
txt_category.ReadOnly = true 跨度>;
txt_category.Enabled = false ;
}
}
解决方案
function sugvalidate(){
var related = document.getElementById(' <% = DropDownList1.ClientID%>')。 value ;
var cat = document.getElementById(' <%= txt_category.ClientID%>')。 value ;
if (related == 10 ){
if (cat == ){
alert( 请指定类别 );
return false ;
}
}
}
嗨Raj,我对你的javascript代码做了一些修改
function sugvalidate(){
var related = document.getElementById(< span class =code-string> <%= DropDownList1.ClientID%>);
var selectedText = related.options [related.selectedIndex] .text;
var cat = document.getElementById( <%= txt_category.ClientID%>)。 value ;
if (selectedText == 其他){
if (cat === < span class =code-string>){
alert( 请指定类别跨度>);
return false ;
}
}
}
希望这可以解决您的问题。 : - )
I have one form in which i have one dropdown and one textbox. This textbox is depends on dropdown selection. when dropdown value is 'Other' then the textbox is enabled otherwise it is disabled. I want to apply javascript validation to the textbox, when textbox is enabled then it should not be emply while submiting the form.
function sugvalidate() {
var related = document.getElementById('<%=DropDownList1.ClientID %>').value;
var cat = document.getElementById('<%=txt_category.ClientID %>').value;
if (related == "Other") {
if (cat == "") {
alert("Please specify Category");
return false;
}
}
}
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="ddcss" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Error/Bug/Issue/Problem" Value="9"></asp:ListItem>
<asp:ListItem Text="Other" Value="10"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txt_category" runat="server" Enabled="False" ReadOnly="True"></asp:TextBox>
<asp:Button ID="btn_submit" runat="server" Text="Submit" OnClientClick="return sugvalidate();" onclick="btn_submit_Click" />
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "Other")
{
txt_category.ReadOnly = false;
txt_category.Enabled = true;
}
else
{
txt_category.ReadOnly = true;
txt_category.Enabled = false;
}
}
解决方案
function sugvalidate() { var related = document.getElementById('<%=DropDownList1.ClientID %>').value; var cat = document.getElementById('<%=txt_category.ClientID %>').value; if (related == "10") { if (cat == "") { alert("Please specify Category"); return false; } } }
Hi Raj, I have done some modifications to your javascript code
function sugvalidate() { var related = document.getElementById("<%= DropDownList1.ClientID %>"); var selectedText = related.options[related.selectedIndex].text; var cat = document.getElementById("<%= txt_category.ClientID %>").value; if (selectedText == "Other") { if (cat === "") { alert("Please specify Category"); return false; } } }
Hope this will solve your problem. :-)
这篇关于文本框中的javascript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文