473,672 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date validation

pradeepjain
563 Contributor
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 1627
Delerna
1,134 Recognized Expert Top Contributor
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 Contributor
use the ltrim function (Use google for specifics)
Change
function ValidateForm(){

//this
var dt=document.frm Sample.txtDate



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






to something like
var dt=ltrim(docume nt.frmSample.tx tDate);


.....

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 Recognized Expert Top Contributor
sorry what i gave you is not java syntax, it is vb/sql syntax

try dt.value.replac e(" ","")

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

try dt.value.replac e(" ","")

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 Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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 Contributor
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.veh icleform.Date
if (isDate(trim_va lue(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
3671
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 seems unnecessarily long. For some while, the following approach has been given here :- function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
0
1987
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 accept on an insert, however, when it does a comparision validation it is failing. I have StartDate = comparing to an invisible textbox that contains todays date EndDate = comparing to StartDate needing to be >= SetupDate = comparing to StartDate...
7
10305
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 cannot preceed the start date. How can I perform the necessary validation using the least number of validation controls. For example is it necessary that I add a requiredfieldvalidator for the start date field and end date field to make sure a value...
7
31825
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 current date. If not, I'd like to display the error message to ValidationSummary. It seems to make sense to me to use CompareValidator but the problem is put the current date into CompareValidator. So, I created a hidden text field in my aspx. ...
12
3278
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 Thanks, Diego.
1
4688
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. <asp:RangeValidator id="varngRollCallDate" runat="server" ErrorMessage="Please enter a valid date in 'dd/mm/yyyy' format." ControlToValidate="txtDate" Display="Dynamic" Type="Date" MinimumValue="01/01/2006" MaximumValue="31/12/2006"...
17
5257
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
2852
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 well. So the control should validate the typed values on the fly. Is it possible?
2
6733
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 I' using the leave event. Right now I am creating a 'leave' sub for each of the fields. However, I'd like to simplify that and just call the name of a function and plug the field name as a variable and be done. In other words, I would like...
5
3437
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 call it. It was suggested to be to have it run through an if statement and if the user enters an invalid date it will kick them to an error page. Here is the date validation code. // date validation using SimpleDateFormat // it will take a...
0
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.