2

I am making a form through laravel in which I want to accept only future dates.I am validating the date via javascript and popping an alert when past date is entered but the problem is that after the alert it is going to form action url (that the form is submitted) even if the past date is entered. I want it to remain to that page itself. Please help.

<html>
<script type="text/javascript">
function func() {
  var aa = document.getElementById('in').value;
  var bb=new Date();
  var y=aa.slice(0,4);
  var m=aa.slice(5,7);
  var d=aa.slice(8,10);
  if((y>=bb.getFullYear() && m>=(bb.getMonth()+1) && d>=bb.getDate())!=1) {
    alert('Enter future date');
  }
}
</script>

<body>
  <form action="/submitted" method="post">
  <input id="in" type="date" name="sdate">
  <input type="submit" onClick="func()"></form>
</body>
</html>
1
  • Will you post a snippet with the problem code? Commented Oct 2, 2015 at 23:11

2 Answers 2

1

use "return false;" after the "alert();" command.

if((y>=bb.getFullYear() && m>=(bb.getMonth()+1) && d>=bb.getDate())!=1)
  {
      alert('Enter future date');
      return false;
  }
Sign up to request clarification or add additional context in comments.

1 Comment

@itsayushbansal Please, mark this answer as the correct answer. Then other peoples will get correct one.
0

Send data with ajax like this:

function func() {
  var aa = document.getElementById('in').value;
  var bb = new Date();
  var y = aa.slice(0, 4);
  var m = aa.slice(5, 7);
  var d = aa.slice(8, 10);
  if ((y >= bb.getFullYear() && m >= (bb.getMonth() + 1) && d >= bb.getDate()) != 1) {
    alert('Enter future date');
  } else {
    var dataString = {
      'date': aa
    };
    $.ajaxSetup({
      headers: {
        'X-CSRF-Token': $('meta[name=_token]').attr('content')
      }
    });
    $.ajax({
      url: "dateroute",
      type: "post",
      data: dataString,
      success: function(data) {
        alert('Good job!');
      }
    });
  }
}

In your routes.php you have to add 'dateroute':

Route::post('dateroute', array('as' => 'dateroute', 'uses' => 'DateController@dateroute'));

Your DateController.php like this:

namespace App\Http\Controllers;
use Request;
use Input;
class DateController extends Controller {

    public function dateroute() {
        if (Request::ajax()) {
            $data = Input::all();
            //here are all datas from ajax datastring
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.