www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to validate date in javascript

  1. #1
    Join Date
    Jul 2014
    Posts
    15

    Question How to validate date in javascript

    Hi,
    this is my javascript code.i'm trying to validate input date.but this form submit when entering wrong date format.

    HTML Code:
    function Validatedate()
    {
    	var chkdate = document.getElementById("fbdy").value
    	if(document.getElementById("fbdy").value == "")
    	{
    		alert("Please enter the Date..!!")
    		if(chkdate.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/))
    		{
    		  alert('date format is correct');
    		}
    		else
    		{
    			alert("date format is wrong")
    		}
    		return false
    	
    	}
    	else{
    		return true
    	}
    }

    HTML Code:
    <form>
    <input type="text" id="fbdy" name="fbdy"/>
    <input type="submit" value="Register" onclick="Validatedate()" />
    </form>
    please help me

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Add return to your onclick event handler function:

    HTML Code:
    <form>
    <input type="text" id="fbdy" name="fbdy"/>
    <input type="submit" value="Register" onclick="return Validatedate()" />
    </form>

  3. #3
    Join Date
    Jul 2014
    Posts
    1
    Tje1,

    your form validation is tucked inside of your if blank statement.. it needs to be pulled out.

    =============================

    function Validatedate()
    {
    var chkdate = document.getElementById("fbdy").value
    if(document.getElementById("fbdy").value == "")
    {
    alert("Please enter the Date..!!")
    return false
    }
    else{

    if(chkdate.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/))
    {
    alert('date format is correct');
    }
    else
    {
    alert("date format is wrong");
    return false;
    }
    return true;
    }
    }

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,407
    IMHO... you nneed to look at the regular expression, try here for an idea http://www.regular-expressions.info/dates.html

    also, your form
    HTML Code:
    <form>
    <input type="text" id="fbdy" name="fbdy"/>
    <input type="submit" value="Register" onclick="return Validatedate()" />
    </form>
    has no name, action or method and should be IMHO
    HTML Code:
    <form name="validate" action="javascript:;" method="post" onsubmit="return Validatedate(this)">
    <input  name="fbdy" type="text" value="" />
    <input name="submit" type="submit" value="Register"  />
    </form>
    and pass the reference to the function via the 'this' object and in addition you should always have three elements to an input field, name, type and value tags even if the value of the field is empty, you should have them tags.

    Your script is them simplified to...
    Code:
    function Validatedate(o)
    {
    	var chkdate = o.fbdy.value || "";
    ...
    the variable chkdate will = the value of the field fbdy or an empty string and the whole form will be available through the object this represented by the variable o in the function which means that if you add more input fields, they are accessible via o.formName.value

    When your form validation works, you can then decide on the forms action.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles