By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

date validation

pradeepjain
100+
P: 563
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
Share this Question
Share on Google+
8 Replies


Delerna
Expert 100+
P: 1,134
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
100+
P: 563
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
Expert 100+
P: 1,134
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
100+
P: 563
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
100+
P: 563
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
Expert 100+
P: 1,134
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
Expert Mod 5K+
P: 5,333
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
100+
P: 563
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

Post your reply

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