- <script language = "Javascript">
-
/**
-
* DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
-
*/
-
// Declaring valid date character, minimum year and maximum year
-
var dtCh= "/";
-
var minYear=1900;
-
var maxYear=2100;
-
-
function isInteger(s){
-
var i;
-
for (i = 0; i < s.length; i++){
-
// Check that current character is number.
-
var c = s.charAt(i);
-
if (((c < "0") || (c > "9"))) return false;
-
}
-
// All characters are numbers.
-
return true;
-
}
-
-
function stripCharsInBag(s, bag){
-
var i;
-
var returnString = "";
-
// Search through string's characters one by one.
-
// If character is not in bag, append to returnString.
-
for (i = 0; i < s.length; i++){
-
var c = s.charAt(i);
-
if (bag.indexOf(c) == -1) returnString += c;
-
}
-
return returnString;
-
}
-
-
function daysInFebruary (year){
-
// February has 29 days in any year evenly divisible by four,
-
// EXCEPT for centurial years which are not also divisible by 400.
-
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
-
}
-
function DaysArray(n) {
-
for (var i = 1; i <= n; i++) {
-
this[i] = 31
-
if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
-
if (i==2) {this[i] = 29}
-
}
-
return this
-
}
-
-
function isDate(dtStr){
-
var daysInMonth = DaysArray(12)
-
var pos1=dtStr.indexOf(dtCh)
-
var pos2=dtStr.indexOf(dtCh,pos1+1)
-
var strMonth=dtStr.substring(0,pos1)
-
var strDay=dtStr.substring(pos1+1,pos2)
-
var strYear=dtStr.substring(pos2+1)
-
strYr=strYear
-
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
-
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
-
for (var i = 1; i <= 3; i++) {
-
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
-
}
-
month=parseInt(strMonth)
-
day=parseInt(strDay)
-
year=parseInt(strYr)
-
if (pos1==-1 || pos2==-1){
-
alert("The date format should be : mm/dd/yyyy")
-
return false
-
}
-
if (strMonth.length<1 || month<1 || month>12){
-
alert("Please enter a valid month")
-
return false
-
}
-
if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
-
alert("Please enter a valid day")
-
return false
-
}
-
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
-
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
-
return false
-
}
-
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
-
alert("Please enter a valid date")
-
return false
-
}
-
return true
-
}
-
-
function ValidateForm(){
-
var dt=document.frmSample.txtDate
-
if (isDate(dt.value)==false){
-
dt.focus()
-
return false
-
}
-
return true
-
}
-
-
</script>
-
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
8 1627 Delerna 1,134
Recognized Expert Top Contributor
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);
-
.....
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;
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
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
- function ValidateForm(){
-
var dt=document.frmSample.txtDate
-
if (isDate( dt.value.replace(" ","") )==false){
-
dt.focus()
-
return false
-
}
-
return true
-
}
hey look i have put ur code in the function near the isDate calling function but not working.....any other idea....
Thanks,
Pradeep
- function ValidateForm(){
-
var dt=document.frmSample.txtDate
-
if (isDate( dt.value.replace(" ","") )==false){
-
dt.focus()
-
return false
-
}
-
return true
-
}
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..
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?
gits 5,390
Recognized Expert Moderator Expert
hi ...
you could use the following function: - function trim_value(val) {
-
var re = /\s/g;
-
-
return val.replace(re, '');
-
}
-
then in your validate-function call: - var dt = trim_value(document.frmSample.txtDate);
kind regards
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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. ...
|
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.
| |
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"...
|
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
|
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?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |