Click here to Skip to main content
15,032,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm using the dd/MM/yyyy format for the date entry and i need to Validate date month year with / separator i want to do that with java script..plz help me
Posted
Comments
Prasad_Kulkarni 24-May-12 23:42pm
   
Not clear.
[no name] 24-May-12 23:49pm
   
@prasad:m asking in the javascript not with custom validator
Technoses 25-May-12 9:08am
   
If you have your solution then please mark it as solution

YOu can use this


JavaScript
function isValidDate(dt)
{
    try
    {
    var myDt=Date.parse(dt)
        if(myDt=='NaN')
        {
            return false;
        }
        else
        {
            var bol;
            
                bol=datevalidation(dt,0)
                //alert(bol);
            if(bol==true)
                return true;
            else
            {   
                
                return false;
            }
        }
        
    }
    catch (e)
    {
        return false;
    }
    
}  
  
function datevalidation(dt,typ)
{
    //alert("Validation")
    var Message="Invalid Date";
    
    
     
    var dts=dt.split('/')
    var m=dt[1];
    
    var d=dts[0];
    var y=dts[2];
    //alert(dt)
    //alert("A4")
    
    if(parseInt(d,10)>31 ||parseInt(m,10)>12 ||parseInt(d,10)==0||parseInt(m,10)==0||parseInt(y,10)==0)  
		{
		     //alert("A5")
			alert(Message);
			//control.select();
			return false;
		}
		else
		{
			 
			if( (parseInt(m,10)=="4"||parseInt(m,10)=="6"||parseInt(m,10)=="9"||parseInt(m,10)=="11") && parseInt(d,10)>30)
			{
			     //alert("A6")
				alert(Message);
				//control.select();
				return false;
			}
			else
			{	  
				if(parseInt(m,10)==2 && parseInt(d,10)>29)
				{
                 //alert("A7")
				alert("Invalid date ");	
				//control.select();
				return false;			
				}
				else
				{
                    //alert("A98")
					if(parseInt(y,10)%4!=0 && parseInt(m,10)==2 && parseInt(d,10)==29)
					{
					    // alert("A8")
					alert(Message);
					//control.select();
					return false;
					}
					else //if ok 
					{ //alert("A41")
					    //alert(y)
						if(y.length==4)
						{
						
						//alert("Success")
						return true;
						}									
							
					} //end else	
					
				} //else after 29 check					

			} //elsa after no of days 30 check
			
		} //else after no of days in month check
//alert("Success0")
    return true;
    
    
    
    
    
}



I you have your solution then please mark it as solution

also you can bookmark it

if any problem then submit your comment
   
Comments
[no name] 25-May-12 2:44am
   
N HOW IT CAB BE CALLED ON BUTTON CLICK?PLZZ REPLY ME
Arunprasath Natarajan 25-May-12 4:59am
   
Since it is Javascript you can call the function isValidDate(dt) OnClientClick. The second function which is mentioned will be called with in function isValidDate(dt)
Technoses 25-May-12 7:30am
   
thanks Arunprasath
Technoses 25-May-12 7:31am
   
Arunprasath Natarajan says right

If you have your solution then please mark it as solution
use this java script then if user enter wrong date then it will not enter alphabet in this text box. if you like this whole js then mark it as answer.

C#
////////////////////////////////////////////////////////////////      Date Textbox Functions

var prevlu;
var invl;
function rtrim(vlu)
{
    var i,j;
    var str="";
    var ch;
    for(i=0;i<vlu.length;i++)
    {
        ch=vlu.substring(i,i+1);
        if(ch!=" ")
        {
            if(ch>=0 || ch<=9 || ch=="/" ) str= str + ch;
        }
    }
    return str;
}

////////////////////////////////////////////////////////////////////////////////

function beforeenter(txtb)
{
    //txtb.
    try
    {
        var dtbox=document.getElementById(txtb);
        if(event.keyCode==9) return;
        if(event.keyCode==13) return;
        if(event.keyCode==8) return;


        dtbox.value=rtrim(dtbox.value);
        if(dtbox.value.length==10)
            dtbox.select();
        prevlu=dtbox.value;
        if(dtbox.value.length==1 && (event.keyCode==111 || event.keyCode==109 || event.keyCode==191 || event.keyCode==189))
        {
            dtbox.value= "0" + dtbox.value;
        }
        if(dtbox.value.length==2 && event.keyCode!=8)
        {
            dtbox.value=dtbox.value + "/";
        }
        if(dtbox.value.length==4 && (event.keyCode==111 || event.keyCode==109 || event.keyCode==191 || event.keyCode==189))
        {
            var dt=dtbox.value.split("/")
            dtbox.value= dt[0] +  "/0" + dt[1];
        }
        if(dtbox.value.length==5 && event.keyCode!=8)
        {
            dtbox.value=dtbox.value + "/";
        }
        if(dtbox.value.length==0 && event.keyCode!=8 && ((event.keyCode>=52  && event.keyCode<=57) || (event.keyCode>=100  && event.keyCode<=105) ))
        {
            dtbox.value=dtbox.value + "0";
        }
        if(dtbox.value.length==3 && event.keyCode!=8 && ((event.keyCode>=52  && event.keyCode<=57) || (event.keyCode>=100  && event.keyCode<=105) ))
        {
            dtbox.value=dtbox.value + "0";
        }
        if(dtbox.value.length==6 && event.keyCode!=8 && event.keyCode==25)
        {
            dtbox.value=dtbox.value + "20";
        }
        invl="F"
        var len=dtbox.value.length;
        var ky=event.keyCode;
        if(len==0 || len==1 || len==3 || len==4 || len==6 || len==7 || len==8 || len==9 || len==10)
        {
            if((ky<48 || ky>57) && (ky <96 || ky>105) && ky!=8  && ky!=17  && ky!=16 )
            {
                event.keyCode=0;
                dtbox.value=prevlu;
                invl="T"
            }
        }
    }
    catch (e)
    {
        dtbox.value='';
    }
}

/////////////////////////////////////////////////////////////////////////////////////


function isValidDate(dt)
{
    try
    {
    var myDt=Date.parse(dt)
        if(myDt=='NaN')
        {
            return false;
        }
        else
        {
            var bol;
            if(dt.length==11)
                bol=datevalidation(dt,1)
            else
                bol=datevalidation(dt,0)
                //alert(bol);
            if(bol==true)
                return true;
            else
            {
                return false;
            }
        }

    }
    catch (e)
    {
        return false;
    }

}

function datevalidation(dt,typ)
{
    //alert("Validation")
    var Message="Invalid Date";

    if(typ==0)
    {

    var dts=dt.split('/')
    var m=dt[1];
    }
    else
    {

     var dts=dt.split('-')
    var m=dts[1];
    m=getmonthstr(m);


    }
    var d=dts[0];
    var y=dts[2];
    //alert(dt)
    //alert("A4")


    //alert(parseInt(d,10));

    if(parseInt(d,10)>31 ||parseInt(m,10)>12 ||parseInt(d,10)==0||parseInt(m,10)==0||parseInt(y,10)==0)
        {
             //alert("A5")
            alert(Message);
            //control.select();
            return false;
        }
        else
        {

            if( (parseInt(m,10)=="4"||parseInt(m,10)=="6"||parseInt(m,10)=="9"||parseInt(m,10)=="11") && parseInt(d,10)>30)
            {
                 //alert("A6")
                alert(Message);
                //control.select();
                return false;
            }
            else
            {
                if(parseInt(m,10)==2 && parseInt(d,10)>29)
                {
                 //alert("A7")
                alert("Invalid date ");
                //control.select();
                return false;
                }
                else
                {
                    //alert("A98");
                    if(parseInt(y)%4!=0 && parseInt(d,10)==29)
                    {
                        // alert("A8")
                        if(parseInt(m,10)=="2")
                        {
                            alert(Message);
                            return false;
                        }
                        return true;
                    }
                    else //if ok
                    { //alert("A41")
                        //alert(y)
                        if(y.length==4)
                        {

                        //alert("Success")
                        return true;
                        }

                    } //end else

                } //else after 29 check

            } //elsa after no of days 30 check

        } //else after no of days in month check
        //alert("Success0")
    return true;





}

function enterdata(txtb)
{
    try
    {
        if(event.keyCode==9) return;
        if(event.keyCode==13) return;
        if(event.keyCode==8) return;

        //if(event.keyCode==46) {alert(event.keyCode); return;}


        var dtbox=document.getElementById(txtb);
        if (invl=="T")
        {
            dtbox.value=prevlu;
        }
        if(dtbox.value.length==2 && event.keyCode!=8)
        {
            if(parseInt(dtbox.value,10)>31)
            {
                dtbox.value=dtbox.value.substring(0,1); // + "/";
            }
        }
        if(dtbox.value.length==2 && event.keyCode!=8)
        {
            dtbox.value=dtbox.value + "/";
        }
        //alert(event.keyCode)

        if(dtbox.value.length==1 && event.keyCode==111)
        {
            dtbox.value="0"+ dtbox.value + "/"

        }

        if(dtbox.value.length==4 && event.keyCode==111)
        {

            var dt=dtbox.value.split("/");

            if(parseInt(dt[1],10)==1)
            {

                dtbox.value=dt[0] + "/0" + dt[1] + "/";
            }
        }

        if(dtbox.value.length==4 && event.keyCode!=8)
        {
            //alert("A")
            var dt=dtbox.value.split("/");
            if(parseInt(dt[1],10)>=2)
            {

                dtbox.value=dt[0] + "/0" + dt[1];
            }
        }


        if(dtbox.value.length==5 && event.keyCode!=8)
        {
            var dt=dtbox.value.split("/");
            if(parseInt(dt[1],10)>12)
            {
                dtbox.value=dt[0] + "/1";
            }
        }
        if(dtbox.value.length==5 && event.keyCode!=8)
        {
            dtbox.value=dtbox.value + "/"
        }
        if(dtbox.value.length==7 )
        {
            var dt=dtbox.value.split("/")
            if(dt[2]=="0")
            {
                dtbox.value= dt[0] +  "/" + dt[1] +  "/20" + dt[2];
            }
            if(dt[2]=="9" || dt[2]=="8"  || dt[2]=="7")
            {
                dtbox.value= dt[0] +  "/" + dt[1] +  "/19" + dt[2];
            }

        }
        if(dtbox.value.length==8 )
        {
            var dt=dtbox.value.split("/")
            if(dt[2].substring(0,1)=="1" && dt[2].substring(1,2)!="9")
            {
                dtbox.value= dt[0] +  "/" + dt[1] +  "/20" + dt[2];
            }
        }
    }
    catch (e)
    {
        dtbox.value='';
    }
}

///////////////////////////////////////////////////////////////////////////////////////////

function onenter(txtb)
{
    var dtbox=document.getElementById(txtb);
    dtbox.selectionStart=1;
    dtbox.selectionLength=1;
    dtbox.focus();
    dtbox.select();
}
///////////////////////////////////////////////////////////////////////////////////////////



in .cs file use this code for setting attributes of textboxes.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        SetDateText(txtDate,txtDate1,txtDate2,txtDate3);
}


C#
public void SetDateText(params TextBox[] txtDateText)
        {
            for (int Count = 0; Count < txtDateText.Length; Count++)
            {
                txtDateText[Count].Attributes.Add("onfocus", "dateonfocus('" + txtDateText[Count].ClientID + "')");
                txtDateText[Count].Attributes.Add("onblur", "dateonleave('" + txtDateText[Count].ClientID + "')");
                txtDateText[Count].Columns = 11;
                txtDateText[Count].Attributes.Add("onkeyup", "enterdata('" + txtDateText[Count].ClientID + "')");
                txtDateText[Count].Attributes.Add("onkeydown", "beforeenter('" + txtDateText[Count].ClientID + "')");
            }
        }
   
Hi,

This JavaScript Will Help You Lot
JavaScript
function  DateValidation()
{
 var validformat = /^\d{2}\/\d{2}\/\d{4}$/
 var txtDate = document.getElementById('<%=txtDate.ClientID %>>');

     if (!validformat.test(txtDate.value)) {
         alert("Invalid Date. Please correct and submit again.");
         txtDate.focus();
         return false;
     }
     else if (validformat.test(txtDate.value)) {
         var Dayfield = txtDate.value.split("/")[0];//Day Field
         var Monthfield = txtDate.value.split("/")[1];//Month Field
         var Yearfield = txtDate.value.split("/")[2];//Year Field

         var newDate = new Date(Dayfield, Monthfield - 1, Yearfield);

         if ((newDate.getMonth() + 1 != Monthfield) ||
              (newDate.getDate() != Dayfield) || newDate.getFullYear()!= Yearfield))
         {
             alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
             txtDate.focus();
             return false;
         }
     }
 }


calling using asp button
C#
<asp:Button runat="server" ID="btnSave" OnClientClick="return DateValidation()" Text="Save" />

calling using html button
HTML
<input id="Button1"  önclick="return DateValidation()" type="button" value="button" />


Hope it helps you
Best Luck
Enjoy Coding:)
   
v2
Comments
[no name] 25-May-12 3:27am
   
hw it cab be called on button click plz tel me
Nilesh Patil Kolhapur 25-May-12 3:31am
   
wait i ll update my solution
[no name] 25-May-12 23:39pm
   
thnx patil
Nilesh Patil Kolhapur 26-May-12 3:24am
   
if this is correct answer for your Question then accept this solution ok.
this will helps to others. ok

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