C# .NET - TextBox Validation Not Working - Asked By Krishna T on 13-Jun-12 08:23 AM

Hi,

I am using Separate Javascript file for Validating the fields But it working for dropdownlist and not working for text box why this problem? The following function working for dropdown and not working for Textbox

Please help me!

Ex:
function Validate() {

    if (document.getElementById('ddlProjectID').selectedIndex == 0) {
      alert("Please Select ProjectID!");
      return false;
    }
if (document.getElementById("<%=txtTaskame.ClientID%>").value == "") {
      alert("Task Name Required!");
      document.getElementById("<%=Taskname.ClientID%>").focus();
      return false;
    }
}
dipa ahuja replied to Krishna T on 13-Jun-12 01:30 PM
 <asp:DropDownList ID="ddlaccesstype" runat="server" ValidationGroup="Chk" Width="171px">
    <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
    <asp:ListItem Text="Admin" Value="1"></asp:ListItem>
    <asp:ListItem Text="Supervisor" Value="2"></asp:ListItem>      
  </asp:DropDownList>
<asp:RequiredFieldValidator ID="reqFavoriteColor" Text="(Required)" InitialValue="0"
    ControlToValidate="ddlaccesstype" runat="server" />
 
Using javascript
 
<script type="text/javascript">
  function Validate() {
    var ddl = document.getElementById('<%=DropDownList1.ClientID%>');
    var option = ddl.options[ddl.selectedIndex];
    alert(option.value);
    if (option.value == 0) {
      alert('select value from dropdown');
      return false;
    }
    else {
      return true;
    }
  }
</script>
  <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate();" />
 
------------------------ for textbox
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="IsEmpty();return false;" />
 
<script type="text/ecmascript">
 
function IsEmpty() {
 
  var name = document.getElementById("<%= TextBox1.ClientID %>").value;
  if (name == "") {
    alert("Name can't be empty");
    name.focus();      
  }
}
</script>
 
 
 
TSN ... replied to Krishna T on 14-Jun-12 12:36 AM

hi

A Small Change in your Code just change the Highligted once..

function Validate() {

if (document.getElementById('ddlProjectID').selectedIndex == 0) {
alert("Please Select ProjectID!");
return (false);
}
if (document.getElementById("<%=txtTaskame.ClientID%>").value == "") {
alert("Task Name Required!");
document.getElementById("<%=Taskname.ClientID%>").focus();
return (false);
}

else return (true)
}

or you can simply mention return instead of return false

Jitendra Faye replied to Krishna T on 14-Jun-12 01:21 AM

For Validating DDL

JavaScript Function

<script type="text/javascript">

function funCheck(src, args) {
var drp= document.getElementById('<%=ddl1.ClientID %>');
if (val.value=="")
{
alert("select any value");
return false;
}
else
{
return true;
}

}
</script>


<div>
<asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem>Select Day</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
</asp:DropDownList>

<asp:Button ID="Button1" runat="server" Text="Button" onClientClick=" return funCheck()"/>

Try this code and let me know.

 

 

Jitendra Faye replied to Krishna T on 14-Jun-12 01:22 AM
For TextBox-

var txt= document.getElementById('<%=TextBox1.ClientID %>');
if (txt.value=="")
{
alert("Enter value");
}



Try this and let me know;.