/    Sign up×
Community /Pin to ProfileBookmark

javascript validation

Hi

I’ve the problem to valdate the date field.

Date: <input name=”txtissue” type=”text” id=”txtissue” size=”16″ maxlength=”8″>
(DD/MM/YY)

how to validate the date.
if any one can enter like 12/34/06.how can i validate the month,like date and year

Please help any one.

Thanks and Regards

Lakshmivijay

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@mrizwanDec 25.2006 — Hi

I've the problem to valdate the date field.

Date: <input name="txtissue" type="text" id="txtissue" size="16" maxlength="8">

(DD/MM/YY)

how to validate the date.

if any one can enter like 12/34/06.how can i validate the month,like date and year

Please help any one.

Thanks and Regards

Lakshmivijay[/QUOTE]


Hi, Download the attached functions file. And include this in your javascript.

Then you will find a function dateCheck(dateStr,formatStr) which can verify all format of dates.

Like you will call it like
[CODE]if(dateCheck(txtissue.value, "%d/%m/%yyyy")==false) alert('Date error');[/CODE]

I hope this helps.

[upl-file uuid=a4347923-9b7b-4d5a-a5bb-804c83ce27d5 size=3kB]functions.zip[/upl-file]
Copy linkTweet thisAlerts:
@lakshmivijayauthorDec 25.2006 — Thanks alot mrizwan.

Its working fine

Thank you very much again.

Regards

Lakshmivijay
Copy linkTweet thisAlerts:
@ricpDec 25.2006 — That's an awful lot of code for very little validation mrizwan. I appreciate it is intended for multiple formats, but still.

A very simple trick to date validation is once you get the m/d/y, pass it into a new date object then compare the .getDate, .getMonth, and .getYear properties of that with the values passed in.

<i>
</i>function isValidDate(d,m,y) {
var dt = new Date(m+"/"+d+"/"+y);
return (dt.getDate()==d&amp;&amp;dt.getMonth()==(m-1)&amp;&amp;dt.getFullYear()==y);
}

alert(isValidDate(1,1,2006)) // true;
alert(isValidDate(29,2,2006)) // false;

Ok, that is overly simplistic, but compared to some completely mad js date checks out there, it's utterly streamlined.



(oh, and why the hell the Americans decided to write the date in the format MM/DD/YYYY is beyond me, they don't tell the time using MM:HH:SS do they? Damned stupid if you ask me)
Copy linkTweet thisAlerts:
@lobsterDec 25.2006 — (oh, and why the hell the Americans decided to write the date in the format MM/DD/YYYY is beyond me, they don't tell the time using MM:HH:SS do they? Damned stupid if you ask me)[/QUOTE]

That is how we say the date: "December 25th, 2006". Month, Day, Year...

why is that so stupid to you? There are many quirks about Great Britain that we find odd or nonsensical .. to say that our way of saying the date is "stupid" says a lot about your acceptance of other cultures... your way isn't always the "better way"... If you grew up with this format it wouldn't sound so strange to you.

Having said that, please explain haggis :p
Copy linkTweet thisAlerts:
@ricpDec 25.2006 — Ah, now being Scottish I can explain Haggis easily. Basically take all the other bits of the animal that aren't good enough to be eaten on it's own but would be a waste to throw away. You have to remember it comes from a culture that was living on the edge of starvation for most of the time.

That said I don't like it. It tastes sh*t! :p


Now as for the date, yes you are totally right regarding the way the date is said, although you could easily give an argument that people, when asked, will say the date is "the 25th of December" rather than "December the 25th".

However this is computers we are talking about here. If you want an incremental date they could set up yyyymmdd which would make ordering easier in both alpha and numeric. Logically you would deal with the smallest set first, the day is more precise than the month, so it makes sense for it to be ddmmyyyy. However I just don't get how it somehow worked out to be mmddyyyy. I'm sure there is a very simple reason, but to me (and everyone else I have asked, both inside and outside of the US) it seems "wrong".
Copy linkTweet thisAlerts:
@mrizwanDec 26.2006 — A very simple trick to date validation is once you get the m/d/y, pass it into a new date object then compare the .getDate, .getMonth, and .getYear properties of that with the values passed in.[/QUOTE]Ricp, you see in original question, a textbox is used for date input. And to use your method, first one will have to split date to day/month & years properly with all possible validations. And it will not remain such easy as you mentioned for a perfect check.
×

Success!

Help @lakshmivijay 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.29,
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,
)...