Date field validation

rss

Posted by coresoul - Apr 20 ’15 at 03:52

Hi, how do i validate a date field for following:

  • empty validation
  • date greater than today
  • date format validation

Thanks

  • davidnarramore
    Member
    Member for: 7 years
    #11 by davidnarramore Apr 28 ’15 at 08:48

    [#10]: Thank you so much Dimitry. This worked great! When would be the best time to do this validation? On submit? would that be fd.onsubmit(    );

  • davidnarramore
    Member
    Member for: 7 years
    #12 by davidnarramore Apr 28 ’15 at 08:50

    [#10]: Thank you so much Dimitry. This worked great! When would be the best time to do this validation? On submit? would that be fd.onsubmit(    );

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 8 years 4 months 2 days
    #13 by Dmitry Kozlov Apr 29 ’15 at 03:48

    [#12]: Hi,

    Yes, if you need to validate the fields before the submission, just put this code into onsubmit handler:

    function getHours(str) {
    	var parts = str.split(' ');
    	if (parts[1] == 'PM') {
    		return parseInt(parts[0]) + 12
    	}
    	
    	parseInt(parts[0])
    }
    
    fd.onsubmit(function() {
    	var dt = fd.field('startDate').value();
    	var dateParts = dt[0].split('/');
    	var startDate = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);
    
    	dt = fd.field('endDate').value();
    	dateParts = dt[0].split('/');
    	var endDate = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);
    
    	if (endDate < startDate) {
    		alert('End Date must be greater than Start Date.');
    		return false;
    	}
    	
    	return true;
    });
    
  • davidnarramore
    Member
    Member for: 7 years
    #14 by davidnarramore Apr 29 ’15 at 06:15

    [#13]: When I put the code in the alert displays as expected. When I change the time so the endDate is greater than the startDate the alert continues to display.

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 8 years 4 months 2 days
    #15 by Dmitry Kozlov Apr 30 ’15 at 12:06

    [#14]: Please, try to trace variables before the comparison. Also, ensure that you don't have JS-errors in the browser console.

    alert(endDate);
    alert(startDate);
    if (endDate < startDate) {
    	alert('End Date must be greater than Start Date.');
    	return false;
    }
    
  • SpongeBen SquareJaw
    Member
    Member for: 7 years 3 months 2 days
    #16 by SpongeBen SquareJaw May 20 ’15 at 12:10

    [#15]: You need to fix the gethours function for this to work put in ; and retun

    function getHours(str) {

    var parts = str.split(' ');

    if (parts[1] == 'PM') {

    return parseInt(parts[0]) + 12;

    }

    return parseInt(parts[0]);

    }

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 8 years 4 months 2 days
    #17 by Dmitry Kozlov May 21 ’15 at 03:00

    [#16]: Exactly! Many thanks.

  • SpongeBen SquareJaw
    Member
    Member for: 7 years 3 months 2 days
    #18 by SpongeBen SquareJaw May 22 ’15 at 07:29

    [#17]:  Actually, I have a correction to my correction. It seems if it is 12 PM and if you add 12 the value would be

    24 so it would add a day. This should be solid now. For DateTime Comparison

     

    function getHours(str) {

    var parts = str.split(' ');

    var partHour = 0;

    if (parts[1] == 'PM') {

    partHour = parseInt(parts[0])

    if (partHour != 12){

    return parseInt(parts[0]) + 12;

    }

    }

    return parseInt(parts[0]);

     

    }

  • Katy
    Member
    Member for: 7 years 6 months 6 days
    #19 by Katy Oct 11 ’16 at 10:08

    I am checking if the date and time field is empty, but can't make it work, here is what i have after reading the above:

    function getHours(str) {

     

    var parts = str.split(' ');

     

    if (parts[1] == 'PM') {

     

    return parseInt(parts[0]) + 12;

     

    }

     

    return parseInt(parts[0]);

     

    }

     

     

     

    fd.onsubmit (function () {

     

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

    if (Boolean(dt[0])) {

     

    var dateParts = dt[0].split('/');

     

    var date = new Date(dateParts[2], dateParts[0]-1, dateParts[1], getHours(dt[1]), dt[2]);

     

    alert('Fill out date');

     

    }

    return false;

     

    }

     

     

     

    return true;

     

    });

     

  • YuriyMedvedev
    Moderator
    Member for: 5 years 3 months 27 days
    #20 by YuriyMedvedev Oct 11 ’16 at 10:44

    [#19]: Hi Katy! Try this one, please:

    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;}
    
    });

     

Displaying 11 to 20 of 28 messages