473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

date validation

pradeepjain
563 512MB
Expand|Select|Wrap|Line Numbers
  1. <script language = "Javascript">
  2. /**
  3.  * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
  4.  */
  5. // Declaring valid date character, minimum year and maximum year
  6. var dtCh= "/";
  7. var minYear=1900;
  8. var maxYear=2100;
  9.  
  10. function isInteger(s){
  11.     var i;
  12.     for (i = 0; i < s.length; i++){   
  13.         // Check that current character is number.
  14.         var c = s.charAt(i);
  15.         if (((c < "0") || (c > "9"))) return false;
  16.     }
  17.     // All characters are numbers.
  18.     return true;
  19. }
  20.  
  21. function stripCharsInBag(s, bag){
  22.     var i;
  23.     var returnString = "";
  24.     // Search through string's characters one by one.
  25.     // If character is not in bag, append to returnString.
  26.     for (i = 0; i < s.length; i++){   
  27.         var c = s.charAt(i);
  28.         if (bag.indexOf(c) == -1) returnString += c;
  29.     }
  30.     return returnString;
  31. }
  32.  
  33. function daysInFebruary (year){
  34.     // February has 29 days in any year evenly divisible by four,
  35.     // EXCEPT for centurial years which are not also divisible by 400.
  36.     return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
  37. }
  38. function DaysArray(n) {
  39.     for (var i = 1; i <= n; i++) {
  40.         this[i] = 31
  41.         if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
  42.         if (i==2) {this[i] = 29}
  43.    } 
  44.    return this
  45. }
  46.  
  47. function isDate(dtStr){
  48.     var daysInMonth = DaysArray(12)
  49.     var pos1=dtStr.indexOf(dtCh)
  50.     var pos2=dtStr.indexOf(dtCh,pos1+1)
  51.     var strMonth=dtStr.substring(0,pos1)
  52.     var strDay=dtStr.substring(pos1+1,pos2)
  53.     var strYear=dtStr.substring(pos2+1)
  54.     strYr=strYear
  55.     if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
  56.     if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
  57.     for (var i = 1; i <= 3; i++) {
  58.         if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
  59.     }
  60.     month=parseInt(strMonth)
  61.     day=parseInt(strDay)
  62.     year=parseInt(strYr)
  63.     if (pos1==-1 || pos2==-1){
  64.         alert("The date format should be : mm/dd/yyyy")
  65.         return false
  66.     }
  67.     if (strMonth.length<1 || month<1 || month>12){
  68.         alert("Please enter a valid month")
  69.         return false
  70.     }
  71.     if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
  72.         alert("Please enter a valid day")
  73.         return false
  74.     }
  75.     if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
  76.         alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
  77.         return false
  78.     }
  79.     if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
  80.         alert("Please enter a valid date")
  81.         return false
  82.     }
  83. return true
  84. }
  85.  
  86. function ValidateForm(){
  87.     var dt=document.frmSample.txtDate
  88.     if (isDate(dt.value)==false){
  89.         dt.focus()
  90.         return false
  91.     }
  92.     return true
  93.  }
  94.  
  95. </script>
  96.  
this is the javascript i am using ..it works properly but if there is a apace at beginning or at last of date it gives "Please enter a valid 4 digit year" which is correct but my client wants the space to be ignored and no msg to be dispalyed ..how to do it..

Thanks,
Pradeep
Jan 16 '08 #1
8 1612
Delerna
1,134 Expert 1GB
use the ltrim function (Use google for specifics)
Change

Expand|Select|Wrap|Line Numbers
  1. function ValidateForm(){
  2.  
  3. //this
  4. var dt=document.frmSample.txtDate
  5.  
  6.  
  7.  
  8. if (isDate(dt.value)==false){
  9. dt.focus()
  10. return false
  11. }
  12. return true
  13. }
to something like

Expand|Select|Wrap|Line Numbers
  1. var dt=ltrim(document.frmSample.txtDate);
  2.  
.....
Jan 16 '08 #2
pradeepjain
563 512MB
use the ltrim function (Use google for specifics)
Change
function ValidateForm(){

//this
var dt=document.frmSample.txtDate



if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}






to something like
var dt=ltrim(document.frmSample.txtDate);


.....

if i use it it escapes the space but the javascript doesnot work only..how to correct it;
Jan 16 '08 #3
Delerna
1,134 Expert 1GB
sorry what i gave you is not java syntax, it is vb/sql syntax

try dt.value.replace(" ","")

this replaces all the spaces with an empty string
Jan 16 '08 #4
pradeepjain
563 512MB
sorry what i gave you is not java syntax, it is vb/sql syntax

try dt.value.replace(" ","")

this replaces all the spaces with an empty string


Expand|Select|Wrap|Line Numbers
  1. function ValidateForm(){
  2. var dt=document.frmSample.txtDate
  3. if (isDate( dt.value.replace(" ","") )==false){
  4. dt.focus()
  5. return false
  6. }
  7. return true
  8. }
hey look i have put ur code in the function near the isDate calling function but not working.....any other idea....

Thanks,
Pradeep
Jan 16 '08 #5
pradeepjain
563 512MB
Expand|Select|Wrap|Line Numbers
  1. function ValidateForm(){
  2. var dt=document.frmSample.txtDate
  3. if (isDate( dt.value.replace(" ","") )==false){
  4. dt.focus()
  5. return false
  6. }
  7. return true
  8. }
hey look i have put ur code in the function near the isDate calling function but not working.....any other idea....

Thanks,
Pradeep


Really srry its working but only for a single space not for multiple spaces ..how to do tht..
Jan 16 '08 #6
Delerna
1,134 Expert 1GB
did you notice that there are no closing semicolons in your isDate function?
is the code making it into the isDate function and then failing or is it failing at the isDate function call?
Jan 16 '08 #7
gits
5,390 Expert Mod 4TB
hi ...

you could use the following function:

Expand|Select|Wrap|Line Numbers
  1. function trim_value(val) {
  2.     var re = /\s/g;
  3.  
  4.     return val.replace(re, '');
  5. }
  6.  
then in your validate-function call:

Expand|Select|Wrap|Line Numbers
  1. var dt = trim_value(document.frmSample.txtDate);
kind regards
Jan 16 '08 #8
pradeepjain
563 512MB
hii Thanks for every ones help..
i finally used this

function trim_value(val) {
var re = /\s/g;
return val.replace(re, '');
}

function validatestring(){
var dt=document.vehicleform.Date
if (isDate(trim_value(dt.value))==false){
dt.focus()
return false
}
return true
}


and it is working fine..Thanks Delerna and gits


Thanks,
pradeep
Jan 16 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
0
by: Brian Conway | last post by:
I am having some validation and insertion problems. I am using a date picker that takes the selected date and puts it to ("dd-MMM-yyyy") format, as this was the only format that Oracle would...
7
by: Paul | last post by:
Hi, I have a form where a user is required to enter a start date and an end date. Both are required and must between a specific date range (e.g. 01/01/1900 and 01/01/2099) and the end date...
7
by: James P. | last post by:
Hello there, In my asp.net page using VB, I have a date text field in mm/dd/yyyy format. When a date is entered, I'd like to validate it to make sure the date is greater than or equal to the...
12
by: Diego | last post by:
Can I validate (possibly with a compare validator) a Date entered by the user based upon his regional settings? I.e. if a user is american the format would be mm/dd/yyyy, if brittish dd/mm/yyyy...
1
by: Brendan Reynolds | last post by:
In an ASP.NET 1.1 app I have the following range validation control. This is an intranet app that will be used only within Ireland, so all date input is expected to be in dd/mm/yyyy format. ...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
3
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I have VS 2005 (C#) There is a control numericUpDown so you can spin numeric values. What I need to do is to spin date (+- one day). How to do that? Moreover, I want a user to type the date as...
2
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
5
Stang02GT
by: Stang02GT | last post by:
I have been asked to validate a date on our web-page so that people cannot enter dates like 14/1/08 or 2/30/06. I have found code that will do exactly what i need it to do, but i am not sure how to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.