santhosha
santhosha

Reputation: 377

Validation not firing in asp.net?

<td>Type:

 </td>
  <td>
    <asp:DropDownList ID="ddltype" runat="server">
     <asp:ListItem>---select---</asp:ListItem>
      <asp:ListItem Text="Setter" Value="1">
       </asp:ListItem>
         <asp:ListItem Text="Getter" Value="2"></asp:ListItem>
           </asp:DropDownList>
            </td>

For this dropdownlist, I put validation like this

var ddltype = document.getElementById('<%=ddltype.ClientID%>');
        var type = ddltype.options[ddltype.selectedValue].value;
        if (type == 0) {
            alert("Please Select setter/getter type.");
            return false;
        }

but it is not firing. How can I write this?

Upvotes: 1

Views: 328

Answers (3)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38703

Try this way ! but you can use asp.net validation control .

Any way ,my solution will validate two type ,for the dropdown selected vale or dropdown selected item

 function Validate()
    {
    var e = document.getElementById('<%=ddltype.ClientID%>');
    //if you need value to be compared then use
    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 setter/getter type.");
        return false;
    }
    }

The below code has change your some code and working good for me

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validation() {
            debugger;
            var e = document.getElementById('<%=ddltype.ClientID%>');
            var strUser1 = e.options[e.selectedIndex].value;
            if (strUser1 == 0) {
                alert("Please Select setter/getter type.");
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="validation();" OnClick="btnSave_Click" />
                    <asp:DropDownList ID="ddltype" runat="server">
                        <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
                        <asp:ListItem Text="Setter" Value="1">
                        </asp:ListItem>
                        <asp:ListItem Text="Getter" Value="2"></asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
        <div>
        </div>
    </form>
</body>
</html>

and see this line

<asp:ListItem Text="--Select--" Value="0"></asp:ListItem>

But you missed set value in that item , So it's get error, Now set value 0 in that line , and now your code working (See my sample code), Or you need to use .text and check the condition for

if(strUser1=="---Select---")
{
//alert
}

Upvotes: -1

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

Try this

var ddltype = document.getElementById('<%=ddltype.ClientID%>').text;
 if (type == "---select---") {
        alert("Please Select setter/getter type.");
        return false;
    }

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460288

You should really get familiar with ASP.NET validators. Javascript can be disabled.

 <asp:DropDownList ID="ddltype" runat="server" AutoPostBack="true">
     <asp:ListItem>---select---</asp:ListItem>
     <asp:ListItem Text="Setter" Value="1"></asp:ListItem>
     <asp:ListItem Text="Getter" Value="2"></asp:ListItem>
 </asp:DropDownList><br />
<asp:RequiredFieldValidator ID="reqType"  runat="server" 
    InitialValue="---select---" 
    ControlToValidate="ddltype"  
    ErrorMessage="Required: Please select a Type" 
    Display="Dynamic"
    CssClass="blah">
</asp:RequiredFieldValidator>

The InitialValue is important. Otherwise ---select--- would be a valid selection.

Note that i've also added AutoPostBack="true" to the DropDownList, maybe you want to postback immediately as soon as the user has selected an item.

Side-note: use a ValidationSummary with ShowMessageBox="true" and EnableClientScript="true" if you want to show the messages in a javascript alert.

Upvotes: 5

Related Questions