Dates

Jun 21, 2011 at 11:41 PM

I'm not sure if the date.split function is part of javascript, jquery or spservices.

I have a simple form with a date field that, via the function PreSaveAction(), need to test that the user's date input is not > than today.

I have this little test I'm doing to see if date.split works for me which was my original question., It seems it is not?

function PreSaveAction()
{
var date = new Date();
var arrDate = date.split("/");
var useDate = new Date(arrDate[2], arrDate[1]-1, arrDate[0]);
alert (date);
alert (useDate);
}

If I comment out arrDate and useDate, I get today's date returned. If I uncomment those two lines, nothing is returned except "Error on page" far left on the status bar. I do have the jQuery libraries referenced on the form as:

<script type="text/javascript" src= jquery.min.js"></script>
<script type="text/javascript" src="............../JQuery/jquery.SPServices.min.js"></script>
<script type="text/javascript" src="............./js/fields.js"></script>
<script type="text/javascript" src="............./js/presave.js"></script>

Am I using SPServices incorrectly?

Thanks

Coordinator
Jun 22, 2011 at 12:16 AM
Carl:

What you're trying to do is straight JavaScript, not jQuery or SPServices.

I think you may have your arguments in the wrong order. Here's an old blog post of mine that might help:
http://sympmarc.com/2009/05/28/validation-on-sharepoint-forms-part-four/
>

M.
Jun 22, 2011 at 7:45 AM

Agree with Mark.

And to the point, another take: I think you're trying to work with an unitialized object, that's why you get the error.

var date = new Date(); alert(date); yields undefined.

Try assigning a valid date to the variable before trying to split a string representation thereof. And check the array length before trying to retrieve at a certain index.

A.K.

Jun 22, 2011 at 2:54 PM

Thanks folks. I did forget to mention I had those variables defined, I just didn't include them, so it would be easier/faster to read. Between js, Jquery and all the different add-ons I'm just getting confused as to what function works with what. I'll look into that part 4 Marc thanks.

Cheers.

CW

Jun 22, 2011 at 4:41 PM
Edited Jun 22, 2011 at 4:52 PM

I tried the Part 4 Validation on SharePoint forms and seemed to have found the culprit. It seems the date1.value.split is not firing for some reason. I added a bunch of alerts to test where it goes wrong in the function. You can see from the comments it stops as soon as we hit the first arrDate var.

From presave.js

function PreSaveAction() {
    var date1 = new Date();
    var date2 = $(":input[title='Date Completed']").val();
    	alert(date1);     //returns "Wed Jun 22 11:28:33 EDT 2011" which is todays date
    	alert(date2);     //returns "6/15/2011" which is what I entered into date field
    var arrDate1 = date1.value.split("/");
    	alert(arrDate1);  //no alert, retuns nothing
    var useDate1 = new Date(arrDate1[2], arrDate1[0]-1, arrDate1[1]);
    	alert(useDate1);  //no alert, retuns nothing
    var arrDate2 = date2.value.split("/");
    	alert(arrDate2);  //no alert, retuns nothing
    var useDate2 = new Date(arrDate2[2], arrDate2[0]-1, arrDate2[1]);
    	alert(arrDate2);  //no alert, retuns nothing
    	alert(date1);     //no alert, retuns nothing
    	alert(date2);     //no alert, retuns nothing
    if(useDate1 > useDate2)
    {
        alert("The End Date cannot happen earlier than the Start Date");  //no alert, retuns nothing

        return false;     //Cancel the item save process
    }
        alert("Ok");      //no alert, retuns nothing
    return true; // OK to proceed with the save item
}

//nothing is returned as arrDateN and useDateN do not seem to be calculated/split.
I have the libraries referenced too:
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<script type="text/javascript" src=".../central/JQuery/jquery.min.js"></script>
<script type="text/javascript" src=".../central/JQuery/jquery.SPServices.min.js"></script>
<script type="text/javascript" src="......./js/fields.js"></script>
<script type="text/javascript" src="......./js/presave.js"></script>

Any ideas? 
Jun 22, 2011 at 5:05 PM

This works howevwer;

function PreSaveAction() {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = (month + "/" + day + "/" + year);
alert(date);
var dateEng= $(":input[title='Date Completed']").val();
alert(dateEng);
if (date<dateEng)
  {
  alert("Must be earlier then to today");
  }
} 

You've got me??

Jun 22, 2011 at 5:29 PM

In case anyone is interested, validating a date field in one of two languages against two dates. Combining and modifying the above code with existing code defining languages and fields, I got this to work.

function PreSaveAction() {

var dateFre= $(":input[title='Date de fin']").val();
var alertFre= "Veuillez choisir une date puis antérieure ou égale à aujourd'hui"
var dateTitleFre= 'Date de fin'

var dateEng= $(":input[title='Date Completed']").val();
var alertEng= "Please choose a date equal to or earlier then today"
var dateTitleEng= 'Date Completed'

var dateCTL  		//we will define this once we find out what language the field "Date Comnpleted" is displayed in
var alertText	//we will define this once we find out what language the field "Date Comnpleted" is displayed in
var dateTitle	//this is used at the end of the script

//even though we know the field's name is "Date Completed", it might not be displaying in english

var dateIDFre= $(":input[title='Date de fin']").attr("id");		//if the form is in French this will return a cryptic ID, not the ordinary 32 character guid
																// otherwise it would return: undefined
var dateIDEng= $(":input[title='Date Completed']").attr("id");	//if the form is in Englidh this will return a cryptic ID, not the ordinary 32 character guid
																// otherwise it would return: undefined
	//alert (dateIDFre);
	//alert (dateIDEng);

//ok now we find out what language the form is displayed in
	if (dateIDFre) {
		//alert ("french");
		dateCTL = dateFre;			//the value of the date when viewed in french
		dateTitle = dateTitleFre;	//the field's nname/label
		alertText = alertFre;
	}
	else {
		//alert ("english");
		dateCTL = dateEng;
		dateTitle = dateTitleEng;
		alertText = alertEng;
	}

		//alert (dateCTL);
		//alert (dateTitle);

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = (month + "/" + day + "/" + year);

		//alert(date);
		//alert(dateCTL);
if (date<dateCTL)
  {
	alert(alertText);
	var fieldConc = $(":input[title='dateTitle']"); 
	fieldConc.focus(); 
	return false;
  }

// if this were only in English we could use: var fieldConc = $(":input[title='Date Completed']"); 

//the save has been stopped
	alert("ok");
	return true; 
} //end function

Please feel free to critique, it only helps me.

Cheers

Carl

 

Coordinator
Jun 22, 2011 at 5:32 PM

Ah. If you're working in a language other than English (at least en-us), then you may well have different formats for things like dates. You always need to debug based on your current environment.

Be sure to always use the terminating ; on your lines of code. Not doing so can produce unexpected results.

M.

Jun 22, 2011 at 5:33 PM

Thanks Marc. Don't tell me I missed that AGAIN!!??