Click here to Skip to main content
15,906,467 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
function isValidDate(sText) {
    var reDate = /(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/;
    return reDate.test(sText);
}
function validateDate(id) {
    //debugger;
    if (document.getElementById(id).value != '') {
        var oInput1 = document.getElementById(id);
        if (isValidDate(oInput1.value)) {
            //By Dileep [03Jan11] to clear colors after correct validation
            document.getElementById(id).style.backgroundColor = "white";
            document.getElementById(id).style.color = "black";
            //End Dileep
        }
        else {
            //By Dileep [03Jan11] to not alert the end user
            // alert("Enter in DD/MM/YYYY");
            document.getElementById(id).value = "";
            //document.getElementById(id).focus();
            //End Dileep
            document.getElementById(id).style.backgroundColor = "#fba09d";
        }
    }
    else {
        //By Dileep [03Jan11] to not allow blank date
        //document.getElementById(id).focus();
        document.getElementById(id).style.backgroundColor = "#fba09d";
        //end dileep
    }
}
//By Dileep Kumar 01Feb2011  -- To Restrict the user to feed Date
function checkDate(str, id) {
    // debugger;
    if (parseInt(document.getElementById(id).value.length) < 10) {
        // ValidInput(str);
        var KeyID = event.keyCode;
        var ch = String.fromCharCode(KeyID);
        if (str.indexOf(ch) > -1)
            return true;
        else
            return false;
    }
    else {
        return false;
    }
}


C# Code-----------------------------------------------------
C#
protected void NameofFormTxt_TextChanged(object sender, EventArgs e)
        {
           
            int i=Convert.ToInt32(ViewState["RowInd"].ToString());
            TextBox ValTextbox = (TextBox)BindTextBoxGv.Rows[i].Cells[1].FindControl("NameofFormTxt");
            if (i == 0)
            {
                ValTextbox.Attributes.Add("onblur", "return validateDate('ctl00$ctl00$ParentContentPlaceHolder$MasterOperationsContentPlaceHolder$BindTextBoxGv$ctl02$NameofFormTxt');");
            }
        }

---------------------------------------------------
My problem is I want to call the above Javascript from the text change event in the following manner but I fail. Is there any other way to call the Javascript?


I also tried in the following way
C#
//----------------------
1.if (i == 0)
            {
                ValTextbox.Attributes.Add("onblur", "return validateDate(this.id);");

//----------------

2.if (i == 0)
            {
                ValTextbox.Attributes.Add("onblur", "return validateDate(<%=NameofFormTxt.ClientID%>);");
}


---------------------------
Please help me
Posted
Updated 15-Feb-11 21:51pm
v2
Comments
Sandeep Mewara 16-Feb-11 3:52am    
Always use PRE tags to format code part. It makes the question readable.

1 solution

Hi as an Example..

I have this text field in the gridview ....

XML
<asp:TemplateField HeaderText="Text Column">
     <ItemTemplate>
         <asp:TextBox ID="Text" runat="server"></asp:TextBox>
     </ItemTemplate>
 </asp:TemplateField>


On the griview I am defining a eventhandler for row databound event like this....(OnRowDataBound="GridView1_RowDataBound")

VB
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="ProductId" DataSourceID="SqlDataSource1"   OnRowDataBound="GridView1_RowDataBound">


on the code behind RowDataBound code like this...

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer)
    {
            TextBox textbox = (TextBox)e.Row.Cells[4].FindControl("Text");
            textbox.Attributes.Add("onblur", "validateText(this)");
    }
}



In the master page header I have the javascript like ....

XML
<script type="text/javascript">
    function validateText(textbox) {
        alert("You entered " + textbox.value);
    }
</script>


Now it can execute the validate script onblur....you can attach this script to another cells in the gridview too.

Hope this helps
 
Share this answer
 
Comments
Sandeep Mewara 16-Feb-11 3:56am    
Good answer and explanation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900