0

At First I use to pass data to action class from java script But I want to do in java script How to do?

Jsp java script:

$('#nwr_fa_dob').keypress(function(event)
       {
          $('#nwr_fa_gender').attr('disabled',false);
         var keycode = (event.keyCode ? event.keyCode :event.which);
         var dob= $('#nwr_fa_dob').val();
         if(keycode == '13' || keycode =='40')
         {
            $.ajax({
                type: "POST",              
                dataType: "json",
                url: "dob_details",
                data: {dob:dob},        
                success: function(response)
                {
                    $('#nwr_fa_dob').val(response.dob);
                    if($('#nwr_fa_dob').val()==null || $('#nwr_fa_dob').val()=='')
                    {
                        alert("You Will Give Age or DateFormat of dd-MMM-yyyy(Ex : 18-Mar-1990)");
                        $('#nwr_fa_dob').val('');
                        $('#nwr_fa_dob').focus();
                        }
                    else
                    {
                        $('#nwr_fa_dob').val(response.dob);
                        $('#nwr_fa_gender').focus();
                        }
                    }
                });
            return false;
            }
         if(keycode=='38'){
             $('#nwr_fa_fsname').focus();
             return false;
             }
         });

Java Code:

public String dob_fetchdate() {
        try {
            if (dob.length() == 11) {
                Date dat = ymd.parse(dob);
                if (ymd.format(dat).equals(dob)) {
                    dob = dob;
                } else {
                dob = null;
                }
            } else if (dob.length() == 10) {
                dob = null;
            } else if (dob.length() == 2) {
                String date = DateNow.substring(7, 11);
                String year = String.valueOf(Long.valueOf(date)
                        - Long.valueOf(dob));
                dob = DateNow.substring(0, 7).concat(year);
            } else {
                dob = null;
            }
            return "success";
        } catch (Exception ex) {
            System.out.println("Dob Error =" + ex);
            return "error";
        }
    }
sathya
  • 1,364
  • 2
  • 14
  • 26

2 Answers2

1

moment.js (http://momentjs.com/) is certainly the reference API for date manipulations in javascript. Here is an example of how it could be used in your case :

var userDateString = "30-Aug-2014";
var dateFormat = "DD-MMM-YYYY";
var myMoment = moment(userDateString, dateFormat);
if (myMoment.isValid()) {
    alert ("Date is valid !");
} else {
    alert ("Invalid date !");
}

It can also tell you what is wrong with the date you typed.

Pear
  • 460
  • 3
  • 15
0

It can easily be done with Regex in javascript

    var date="01-01-1944";
    var pattern = /[0-9]{2}[-]{1}[0-9]{2}[-]{1}[0-9]{4}/i;

    if(pattern.test(date)) {
        var dateParts = date.split("-");
        // You can do the range checking here
        console.log(dateParts[0]);
        console.log(dateParts[1]);
        console.log(dateParts[2]);
    }

To know more about Regex in JS follow this

Also read answers of this question Regular Expression to match valid dates.

Community
  • 1
  • 1
mirmdasif
  • 5,008
  • 2
  • 20
  • 27