views:

109

answers:

3

Hi,

I have created my own code to provide date masking and validation for TextBox control in asp.net. Below is the code. The code works perfectly.

function IsValidDate(ctrlID) { var validDate=true;

    var myT=document.getElementById("ctl00_ContentPlaceHolder1_CandidateResume1_TabContainer1_TabPanel2_Education1_"+ctrlID);
    var mm=myT.value.substring(0,2);
    var dd=myT.value.substring(5,3);
    var yy=myT.value.substring(6);

    if(mm!=0 && mm>12){
        myT.value=""; validDate=false;
    }
    else
    {
        if((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
        {
            if(mm==2 && dd>29){
                myT.value=""; validDate=false;
            }
        }
        else
        {
            if(mm==2 && dd>28){
                myT.value=""; validDate=false;
            }
            else
            {
                if(dd!=0 && dd>31){
                    myT.value=""; validDate=false;
                }
                else
                {
                    if((mm==4 || mm==6 || mm==9 || mm==11) && (dd!=0 && dd>30)){
                        myT.value="";  validDate=false;
                    }
                }
            }
        }
    }

    if(validDate==false)
    {
        myT.style.backgroundColor='#FF0000';
        myT.focus;
    }
    else
        myT.style.backgroundColor='#FFFFFF';
}

function maskDate(ctrlID)
{
    var myT=document.getElementById("ctl00_ContentPlaceHolder1_CandidateResume1_TabContainer1_TabPanel2_Education1_"+ctrlID);
    var KeyID = (window.event) ? window.event.keyCode : 0;        
    if((KeyID>=48 && KeyID<=57) || KeyID==8)
    {
        if(KeyID==8)
            return;

        if(myT.value.length==2)
        {
            myT.value=myT.value+"/";
        }
        if(myT.value.length==5)
        {
            myT.value=myT.value+"/";            
        }
    }
    else
    {
        window.event.keyCode=0;
    }

}

The problem -

I am attaching these functions to the textbox as - TextBox1.Attributes.Add("onFocusout","IsValidDate('TextBox1');"); TextBox1.Attributes.Add("onKeyPress","maskDate('TextBox1');");

If you look at the javascript code I have collected the control id in myT variable. I have also passed the id of textbox while attaching the js functions using Attributes.Add()

My problem is that i dont want to pass the id of the textbox as i am already attaching it. That is i want to write the code as

TextBox1.Attributes.Add("onFocusout","IsValidDate();"); TextBox1.Attributes.Add("onKeyPress","maskDate();");

My question is how can i get the id of textbox to which i have attached these functions witin JS code.

NOTE: I DONT WANT TO PASS CONTROL NAME OR CONTROLS CLIENTID WHILE ADDING ATTRIBUTES. PLEASE NOTE THAT I WANT TO REPLACE

TextBox1.Attributes.Add("onFocusout","IsValidDate('TextBox1');"); WITH TextBox1.Attributes.Add("onFocusout","IsValidDate();"); I WANT TO ATTACH THESE FUNCTIONS WITH MULTIPLE TEXTBOXES.

AS I AM USING .Attributes.Add(...) I WANT TO GET THE SAME CONTROLS CLIENTID WITHIN JS CODE.

Your help is highly appreciated.

Thanks and Regards Mohammad Irfan

A: 

Control.ClientID

Neil N
My friend, i am asking about the id of control within javascript code not in aspx code.
IrfanRaza
And ClientID gives you exactly that
Neil N
A: 

Either pass TextBox1.ClientID to the function, or change the function call to be IsValidDate(this.id). But as you don't want to pass these in, you can place the TextBox1.ClientID in your javascript or use jquery to find it using $('[id*=TextBox1]').

Yuriy Faktorovich
Thanks buddy!!! but please read my question carefully.
IrfanRaza
+1  A: 

var txtControl = document.getElementById("<%= txtControl.ClientID %>");

Kenneth J