Go Back   DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Sponsored Links

Discuss "date validation" in the JavaScript Programming forum.

JavaScript Programming - Javascript is a cross-browser client-side scripting language. Discuss Javascript and AJAX related scripts here.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old July 5th, 2008, 10:05 PM
Barn Frequenter

 
Join Date: Mar 2008
Posts: 197
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
todd2006 is an unknown quantity at this point
Default date validation

Hi,

I have a textbox called insexpdate

I want the user to enter valid date format in mm/dd/yyyy format only

I call the function onblur event

Code:
function callinsexpdate()
{
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
alert(re);
  if((document.frm1.insexpdate.value == "") && (!document.frm1.insexpdate.value.match(re)))
  { 
	  alert("Invalid date format"); 
	
	  return false;
  }



}
but the code is not working

any idea

todd
Reply With Quote
Sponsored Links
  #2  
Old July 6th, 2008, 08:52 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Try this function:

Code:
function checkdateformat(userinput){
var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
return dateformat.test(userinput) //returns true or false depending on userinput
}
The above function returns a boolean (True or False) value depending on if the date entered is in the correct format or not.
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old July 6th, 2008, 11:34 AM
Barn Newbie

 
Join Date: Mar 2008
Posts: 40
Thanks: 0
Thanked 4 Times in 4 Posts
Rep Power: 1
lewy is on a distinguished road
Default

@jmh - that will work, but how do you know it's a correct date? It's more complicated than that
I could just type in 2/31/0000 and it will validate

In my opinion, giving users the ability to type in a date is a bad idea as they will make mistakes from time to time.

Rather than doing the validation this way, why don't you use a calendar picker.
There are plenty out there on the net.
Make sure the textbox holding the value is readonly

Then on the serverside, you can validate that it's a correct date (this because you just can't trust users)

Besides, it's a good programming practice to always validate input on the server.
Reply With Quote
  #4  
Old July 6th, 2008, 11:39 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

I completely agree. Just answering the question that was asked. But, to take it one step further, as lewy stated, using a date picker which only allows selection of valid dates would be far better
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > JavaScript Programming

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
textbox validation todd2006 JavaScript Programming 6 July 2nd, 2008 01:30 PM
checkbox validation todd2006 JavaScript Programming 3 June 18th, 2008 07:22 PM
[ASP.Net/VB.Net] Programmatically Add Item to Validation Summary jmurrayhead Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 03:46 PM.



Content Relevant URLs by vBSEO 3.2.0