Is there any function in JavaScript which validates the date format like isNaN() for numbers? Or have anyone made such function which validates the date entered in the text box i.e. dd-mm-yyyy or yyyy-mm-dd or mm-dd-yyyy or dd/mm/yyy or mm/dd/yyyy or yyyy/mm/dd are only the valid dates??
Recently while messing with dates, I noticed an odd quirk in javascript with new Date(), i.e. if someone enters an invalid date, such as 2/29/2003, javascript creates the new date as 3/1/2003.
Having a look around, I couldn't find any scripts that took advantage of this for the sake of date validation... probably someone here has done this before, but I'll post it anyway.
The idea is that if javascript creates a new Date() with a different month, then obviously the date entered is not valid. Most of the scripts I saw used some math to divide by leap year, yadda yadda yadda, but with this feature (?) of javascript, it seems unnecessary.
Right now this code only validates mm/dd/yyyy, but it should be easy to modify to support other formats:
function isDate(sDate) { var re = /^d{1,2}/d{1,2}/d{4}$/ if (re.test(sDate)) { var dArr = sDate.split("/"); var d = new Date(sDate); return d.getMonth() + 1 == dArr[0] && d.getDate() == dArr[1] && d.getFullYear() == dArr[2]; } else { return false; } }
Here's a shorter version that works if you pass in the values separately:
function isDate(mm,dd,yyyy) { var d = new Date(mm + "/" + dd + "/" + yyyy); return d.getMonth() + 1 == mm && d.getDate() == dd && d.getFullYear() == yyyy; }
I have written a javascript to compare current date with the date that user has entered in the form. the user enters in YYYY-MM-DD format. here'z the code:
var today= new Date(); var stDate= new Date(document.form[0].startDate.value); var day=0; var month=0; var year=0; var todayStr; day= today.getDate(); month= today.getMonth()+1; year= today.getFullYear(); todayStr= new Date(year + "-" + month + "-" + day); alert(todayStr); if(todayStr>stDate) { alert("Current date is greater");}
but todayStr gives NaN . and comparison gives no result.
I am trying to use javascript code so that on loading of a webpage, the page displays the day, date, month and year words. So far I have come up with the following but it doesnt seem to be working: Code:
Im trying to validate my date textbox on my form to only allow mm/dd/yy. If the user enters anything else in I want it to display an error message and focus on the date textbox. Ive looked at many sample scripts with no luck. Anyone out there done this before, and possibly still have the code?
I have a date function that subtract a certain amount of days from todays date.
Code: function printDate(offset) {
var offsetAmount= offset var currentTime= new Date() var month= currentTime.getMonth() + 1 var day= currentTime.getDate() var nDay= day - offsetAmount var year= currentTime.getFullYear()
document.write(nDay + "/" + month + "/" + year)
} I need it to be able to recognise when it goes into a previous month (ie instead of 0/6/2006 it will go to 31/5/2006)
I have a start date and end date text boxs. What I would like to achieve is when a submit button is clicked all the available dates between start and end dates should be displayed together with 3 check boxes next to each date (please see below). I am just wondering whether that'sachievablewith jquery, and if so, how I might be able to implement this.
I'm looking to return DATE ONLY for yesterday's date. No seconds, milliseconds. Formatted either yyyy/mm/dd or mm/dd/yyyy. VB does it so easily Date()-1 will return 03/27/2007 if today is 03/28/2007. Why so many hoops for javascript? Any ideas?
Apparently, ` new Date() ` reads it correctly, though problems can occur if the browser returns only two digits for the year. In particular, time zone, field order and separators may vary. It is also reliant on the server's clock having been correctly set at the time of upload. See the URL below.
Okay, this an attempt to port PHP's date() function as much as possible to JavaScript. Could use some refactoring though. Any critique, comments, appraisal and any other opinion is very welcome. Feel free to discuss and also take a look at beetles code here: http://www.codingforums.com/showthread.php?s=&threadid=11069
Oh, and the date() function is described here: http://www.php.net/manual/en/function.date.php
Date.prototype.format = function (formatStr) { var heap = formatStr.split(""); var resHeap = new Array(heap.length); var escapeChar = ""; // you can change this to something different, but // don't use a character that has a formatting meaning, // unless you want to disable it's functionality
// go through array and extract identifiers from its fields for (var i = 0; i < heap.length; i++) { switch(heap[i]) { case escapeChar: resHeap[i] = heap[i+1]; i++; break;
case "a": // "am" or "pm" var temp = this.getHours(); resHeap[i] = (temp < 12) ? "am" : "pm"; break;
case "A": // "AM" or "PM" var temp = this.getHours(); resHeap[i] = (temp < 12) ? "AM" : "PM"; break;
case "d": // day of the month, 2 digits with leading zeros; i.e. "01" to "31" var temp = String(this.getDate()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "D": // day of the week, textual, 3 letters; i.e. "Fri" var temp = this.dayNames[this.getDay()]; resHeap[i] = temp.substring(0, 3); break;
case "F": // month, textual, long; i.e. "January" resHeap[i] = this.monthNames[this.getMonth()]; break;
case "g": // hour, 12-hour format without leading zeros; i.e. "1" to "12" var temp = this.getHours(); resHeap[i] = (temp <= 12) ? temp : (temp - 12); break;
case "G": // hour, 24-hour format without leading zeros; i.e. "0" to "23" resHeap[i] = String(this.getHours()); break;
case "h": // hour, 12-hour format; i.e. "01" to "12" var temp = String(this.getHours()); temp = (temp <= 12) ? temp : (temp - 12); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "H": // hour, 24-hour format; i.e. "00" to "23" var temp = String(this.getHours()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "i": // minutes; i.e. "00" to "59" var temp = String(this.getMinutes()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "I": // "1" if Daylight Savings Time, "0" otherwise. Works only on the northern hemisphere var firstDay = new Date(this.getFullYear(), 0, 1); resHeap[i] = (this.getTimezoneOffset() != firstDay.getTimezoneOffset()) ? (1) : (0); break;
case "J": // day of the month without leading zeros; i.e. "1" to "31" resHeap[i] = this.getDate(); break;
case "l": // day of the week, textual, long; i.e. "Friday" resHeap[i] = this.dayNames[this.getDay()]; break;
case "L": // boolean for whether it is a leap year; i.e. "0" or "1" resHeap[i] = (this.getFullYear() % 4) ? false : true; break;
case "m": // month; i.e. "01" to "12" var temp = String(this.getMonth() + 1); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "M": // month, textual, 3 letters; i.e. "Jan" resHeap[i] = this.monthNames[this.getMonth()]; break;
case "n": // month without leading zeros; i.e. "1" to "12" resHeap[i] = this.getMonth() + 1; break;
case "O": // Difference to Greenwich time in hours; i.e. "+0200" var minZone = this.getTimezoneOffset(); var mins = minZone % 60; var hour = String(((minZone - mins) / 60) * -1);
case "r": // RFC 822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200" var dayName = this.dayNames[this.getDay()].substr(0, 3); var monthName = this.monthNames[this.getMonth()].substr(0, 3); resHeap[i] = dayName + ", " + this.getDate() + " " + monthName + this.format(" Y H:i:s O"); break;
case "s": // seconds; i.e. "00" to "59" var temp = String(this.getSeconds()); resHeap[i] = (temp.length > 1) ? temp : "0" + temp; break;
case "S": // English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th" var temp = this.getDate(); var suffixes = ["st", "nd", "rd"]; var suffix = "";
case "t": // number of days in the given month; i.e. "28" to "31" resHeap[i] = this.getDay(); break;
/* * T: Not implemented */
case "U": // seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) // remember that this does not return milisecs! resHeap[i] = Math.floor(this.getTime() / 1000); break;
case "w": // day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday) resHeap[i] = this.getDay(); break;
case "W": // ISO-8601 week number of year, weeks starting on Monday var startOfYear = new Date(this.getFullYear(), 0, 1, 0, 0, 0, 0); var firstDay = startOfYear.getDay() - 1;
case "y": // year, 2 digits; i.e. "99" resHeap[i] = String(this.getFullYear()).substring(2); break;
case "Y": // year, 4 digits; i.e. "1999" resHeap[i] = this.getFullYear(); break;
case "z": // day of the year; i.e. "0" to "365" var firstDay = Date.UTC(this.getFullYear(), 0, 0); var thisDay = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()); resHeap[i] = Math.floor((thisDay - firstDay) / (1000 * 60 * 60 * 24)); break;
case "Z": // timezone offset in seconds (i.e. "-43200" to "43200"). resHeap[i] = this.getTimezoneOffset() * 60; break;
I have date(ex., Thu, 04 Dec 2003 10:35:19 +0500) and I know the timezone(ex., America/Anchorage). Now I need to convert the date into the given timezone. Is it possible to achieve this thru js api's?
I've browsed through past usenet archives, but can't seem to come across quite the javascript I'm looking for. I'm looking for a simple javascript that will display the date as such:
May 17
So basically, just displaying the current month and the current date. But I would also like the ability to backdate by one day, two days, etc.. So the next date might look as such:
May 15
Which would be two days earlier than today's date, but in keeping with the same format.
I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere.
Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account?
I'm calculating the difference in the usual way, i.e....
var difference = dateTo.getTime() - dateFrom.getTime();
....and converting this millisecond value into days by using...
var daysDifference = (difference/1000/60/60/24);
But how do I then display the difference in days AND years? I've tried the following:
var yearsDifference = Math.floor(daysDifference/365.25); var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25));
....but it gives me inaccuracies. For example, if I use my code to calculate the difference between 05/01/1998 and 05/01/2000 it returns 1 year and 364 days!
I am looking for HTML validator with the following restrictions: 1. Web server is the localhost (page should be validated locally). 2. The page is dynamic (generated by PHP with client side javascript, which alters the DOM).
I tried the following:
1. Tidy Firefox extenstion (http://users.skynet.be/mgueury/mozilla/). Unfortunately, it doesn't really makes the real DOM validation. In my JS code, I had .inerHTML code injection, but this extension didn't show the injected html code.
2. I am using FireBug Firefox extenstion. This extenstion shows the real DOM, but unfortunately it doesn't validate the HTML. 3. MS developper toolbar for IE and Web Developper Firefox extenstion make only external HTML validation.
/* With RegExp */ function isEmail2(who) { var email=/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*.([A-Za-z]){2,4}$/i; return(email.test(who)); } </script>
Everyone around here sure seems to be doing lots of form validation. Fortunately, I have your solution. I've made a form validator that does LOTS AND LOTS of neat stuff. It's still 'beta' because I'm not done with x-browser 100% etc. Well, you can get all the dope here ....
Trying to make a simple validator for a form i've just created, but for some reason i cant get it to redirect to the pages upon the IF statements being fulfilled.I've got a feeling its because the form seems to still submit the selection...
Using function FrontPage_Form1_Validator(theForm) and added a function to trap a radio optin like
if (theForm.opt_in.value != "yes" || theForm.opt_in.value != "no") { alert("Please make a choice for the "opt_in" field."); theForm.opt_in.focus(); return (false); }
This traps if neither radio has been clicked BUT when I click on OK in the error message box it continues on without having click on a radio button?
Is the action of clicking the OK in the error message setting to True or something? Any idea of a fix?
I'm trying to write a generic/reusable form validator in Javascript... just something that checks to make sure required fields have a value. By generic I mean I don't want to explicitly reference the name/id of the form or the name of any of the data fields within a "validation" function.
if(problemFields.length > 0) { returnval = false; warn(problemFields); /* tells user they're missing a field, that's all */ }
return returnval; }
What I think is happening (not sure) is that the expression form[fieldsToValidate[i]] is not giving me what I want: a reference to the object corresponding to the form field with the same name. In otherwords, I must have some fundamental misunderstanding of how the DOM works here. Unfortunately, I can't seem to find a good enough reference to set me straight....
I'm quite new to Jquery, so I need some sort of advice. I'm working on a webbased application with a webservice. First it was a simple html site, but later I started using Jquery. Data is inserted in some forms, this I can easily validate through the validator. Now after all the data is ok, it's send to the webservice through the C#-code. Some calculations are being made with the data and a dataset is returned. Now some errors can occur while doing the calculations. I can't get some manual error in the errorbox of the validator. Is it possible to let the validator run some c#-function so I can see if the data is correct? Or can I set a unvisible checkbox to true/false through the c#-code so the validator check this box? A problem with both is, that the validator runs when I click on the "Next"-button. After the validaton, the function starts, so if I set the checkbox to false, the validator won't even notice. Anyone knows a handy approach for this?
I've seen tons of things for fixing it with IE6 but some of my users are getting this with IE8.I don't see too many answers to problems with this plugin is it not being supported anymore?I also see that the version on the jQuery site is 1.5.5 and the version on the author's site is 1.8.1; does this mean that the author isn't keeping things here updated and thus isn't supporting the product via these channels like he says on his site?
I wanted to know if we have some validator tool available which can validate our jsp code for W3C standards and for cross browser compatibility for browsers like IE, Mozilla, Chrome.