Date field validation

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
YuriyMedvedev
Moderator
Posts: 33
Joined: Wed Sep 21, 2016

11 Oct 2016

Hi Katy! Try this one, please:

Code: Select all

fd.onsubmit (function () {

  var dt = fd.field('DueDate').value()

  if (Boolean(dt[0])) {

  alert('Date is filled!');

  return true;

 }

 else{ alert('Fill the date, please');

 return false;}

});

Katy
Posts: 145
Joined: Wed Dec 02, 2015
Location: Canada

11 Oct 2016

Hello Yuriy!

Thanks this is working. I have a few date and time fields (12) in my form actually and i need to validate each one.That means i need to set 12 different var-s, right?

YuriyMedvedev
Moderator
Posts: 33
Joined: Wed Sep 21, 2016

12 Oct 2016

Hi! Here is an example for two fields.

Code: Select all

fd.onsubmit (function () {
  if (!fd.field('DueDate').value()[0]) {
  alert('Date has not been filled!');
  return false;
 }
 if (!fd.field('AnotherDate').value()[0]) {
  alert('Another Date has not been filled!');
  return false;
 }
return true;
});

Katy
Posts: 145
Joined: Wed Dec 02, 2015
Location: Canada

12 Oct 2016

I am actually trying to make fields conditionally mandatory as described here: http://spform.com/office-365/cond ... ynamically

The problem is i have two coditions but even with one it is not working so here is my code:

fd.onsubmit (function () {
var dt = fd.field('DueDate').value()
var sdt = fd.field('SetDate').value()

if (fd.field('Ready').value()) {
if (fd.field('check1').value()) {
if (Boolean(dt[0]) && Boolean(sdt[0])) {
return true;
}
else{ alert('Fill the date, please');
return false;}
}
}
});



The date fields showld be mandatory only if 2 checkboxes are checked. I also tryed:

if (fd.field('Ready').value() && fd.field('check1').value())



like this:

fd.onsubmit (function () {
var dt = fd.field('DueDate').value()
var sdt = fd.field('SetDate').value()

if (fd.field('Ready').value() && fd.field('check1').value()) {

if (Boolean(dt[0]) && Boolean(sdt[0])) {
return true;
}
else{ alert('Fill the date, please');
return false;}
}
});



It is giving the alert when the checkboxes are checked, but if they are not checked i can't save the item at all. I am issing something for sure here....

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

12 Oct 2016

Yes, you have to return true in the end of your onsubmit handler.

Katy
Posts: 145
Joined: Wed Dec 02, 2015
Location: Canada

12 Oct 2016

You mean put one more

return true

at the end?

Or just change places true and false in my script and change the dt[0] to dt [1]? sorry, i am learning everything myself :-)

YuriyMedvedev
Moderator
Posts: 33
Joined: Wed Sep 21, 2016

13 Oct 2016

Yes, after all the condition-blocks (if(){}), in the end of function, before '})' you have to put "return true".

Code: Select all

fd.onsubmit (function () {
var dt = fd.field('DueDate').value()
var sdt = fd.field('SetDate').value()

if (fd.field('Ready').value() && fd.field('check1').value()) {

 if (Boolean(dt[0]) && Boolean(sdt[0])) {
  return true;
}
 else{ alert('Fill the date, please');
  return false;}
}
return true;
});

Katy
Posts: 145
Joined: Wed Dec 02, 2015
Location: Canada

13 Oct 2016

Thanks a lot! Works great!

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 7 guests