/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=html]
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
}
}
[/code]

[code=html]
<form>
<input type=”text” id=”fbdy” name=”fbdy”/>
<input type=”submit” value=”Register” onclick=”Validatedate()” />
</form>
[/code]

please help me

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@tech_soul8Jul 29.2014 — Add return to your onclick event handler function:

[code=html]
<form>
<input type="text" id="fbdy" name="fbdy"/>
<input type="submit" value="Register" onclick="return Validatedate()" />
</form>
[/code]
Copy linkTweet thisAlerts:
@KustomeJul 29.2014 — 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;
}

}
Copy linkTweet thisAlerts:
@rootJul 30.2014 — IMHO... you nneed to look at the regular expression, try here for an idea http://www.regular-expressions.info/dates.html

also, your form [code=html]<form>
<input type="text" id="fbdy" name="fbdy"/>
<input type="submit" value="Register" onclick="return Validatedate()" />
</form>[/code]


has no name, action or method and should be IMHO [code=html]<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>[/code]
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 || "";
...
[/CODE]

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.
×

Success!

Help @Tje1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...