wood burning stoves*
The moose likes HTML, CSS and JavaScript and the fly likes date validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "date validation" Watch "date validation" New topic
Author

date validation

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

hi pals,

first of all i dont have any information about write code with javascript and i need to to some validation on a date ,i wanna check if is the from date & to date in this format or no YYYY-MM-DD..
Any help ??


Thanks,
Sherif
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

What have you written so far? Do you know anything about regular expressions? How to write an event handler that can perform the check? Obtain the field values?

You'll need to write some code and then come here for help with the parts that have you flummoxed.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

i know all that but some pals told me it's much easier with javascript. is this right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

I was talking about JavaScript.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Also, be aware that performing validation on the client using JavaScript cannot replace server-side validation. Client-side validation is nice for letting the user know that an error has been made at the earliest opportunity, but server-side validation must still be performed.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

no i wanna do the validation on the client side this why i went for javascript..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

And what if someone turns off JavaScript? What is someone copies your form and submits their own data without your validation? Are you prepared for cross-site scripting attacks?

Data validation must occur on the server in addition to any client-side validation. You can never rely solely on just client-side validation.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

oppps , these things never came to my mind may be because this will be internal application and never accessed from outside our network but in general you are right..you you recommend client side and server side validation or just server side?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

I prefer both. Server-side validation for the "real" validation, and client-side validation to make things nice for the user.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Ok , the server side i can do it , but about the client side as i told you i don't know javascript so can you help me in this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

First, you need to decide at what point you want to validate. Customarily, validation happens at form submission time, but sometimes fields are validated as soon as the user tries to leave them.

Once you decide on that, you need to figure out how to handle the events that are triggered at those points.

Event handling is not an easy introductory JavaScript topic because it is the area in which IE is the most non-standard, and so you need to write lots of cross-browser code to handle the events on all browsers.

To be honest, I'd learn a bit more about JavaScript before tackling this. But if you want to jump in feet first, I'd recommend reading the chapter on Events that I wrote for Ajax in Practice. It's one of the freely available sample chapters for the book. You'll find it here. Click on the link for Sample Chapter 5.

After getting an idea of how painful it is to handle events on your own, it's best to adopt one of the JavaScript libraries that handles the cross-browser nuances for you. The sample chapter shows how to use Prototype for this, but jQuery also has really good event handling APIs (in fact, a bit more powerful than Prototype's).

Once you've got the event handling down, then it's time to go on to the next subject: grabbing and validating the data.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Sherif,
Don't trust those internal users too much either. What happens if you have a disgruntled employee who decides to wreck the database? The fact that they could enter - say a SQL query - in your text field and run a delete would be a really bad thing.

Bear,
I understand how event handling in AJAX isn't an easy introductory topic. But what's so hard or browser specific about onblur or onsubmit? I'm assuming Sherif isn't using anything more complicated if he doesn't know much JavaScript.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Even the simplest event handling needs to deal with the differing Event instances and the manner in which they are obtained.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Originally posted by Bear Bibeault:
Even the simplest event handling needs to deal with the differing Event instances and the manner in which they are obtained.


Even something as simple as this:


Sorry for the thread tangent. I'm really trying to understand what complexity I'm missing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Your handler is hard-coded to check a specific field. Most forms have many fields that need to be checked, and the only way to create a common handler would be to use the Event instance to discover the triggering component.

Sure, there are trivial cases where you may not run up against browser differences, but as soon as event handling needs to do anything of substance, those differences are right there in your face -- I have the lack of hair to prove it!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30353
    
150

Originally posted by Bear Bibeault:
Your handler is hard-coded to check a specific field.

Fair enough. I guess my point is that Sherif could hard code his date field to get going before he understands all the details of event handling.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Originally posted by Jeanne Boyarsky:

Fair enough. I guess my point is that Sherif could hard code his date field to get going before he understands all the details of event handling.


Ya , and i prefer that too specially i got strict time frame, Thanks pals for your support...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

In that case, you can start with Jeanne's example and adapt it to your own use. You will find that JavaScript regular expressions will be useful to test the input against a pattern.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date validation