Jak automatycznie formatować wejścia tekstowe

<tr>
   <td><label>Birthdate</label>
      <input type="text" placeholder="mm/dd/yyyy" name="birthdate" maxlength="10"/>
   </td>
</tr>

Cóż, mój kod działa, ale chcę, aby mój „tekst typu wejściowego” był automatycznie formatowany jak data (typ wejścia html 5 = data), ponieważ w moim Servletu konwertuję go na Age. Problem polega na tym, że jeśli użyję „input type = date”, konwersja jest błędem, więc postanowiłem użyć „input type = text” i działa. Czy jest możliwe automatyczne wstawienie „/” w tym formacie „mm / dd / rrrr”? Na przykład, jeśli użytkownik wprowadzi 2 znaki, „/” automatycznie wprowadzi itd.

Serwlet na datę urodzin do wieku

        String birthdate = request.getParameter("birthdate");


        int monthDOB = Integer.parseInt(birthdate.substring(0, 2));
        int dayDOB = Integer.parseInt(birthdate.substring(3, 5));
        int yearDOB = Integer.parseInt(birthdate.substring(6, 10));

        DateFormat dateFormat = new SimpleDateFormat("MM");
        java.util.Date date = new java.util.Date();
        int thisMonth = Integer.parseInt(dateFormat.format(date));

        dateFormat = new SimpleDateFormat("dd");
        date = new java.util.Date();
        int thisDay = Integer.parseInt(dateFormat.format(date));

        dateFormat = new SimpleDateFormat("YYYY");
        date = new java.util.Date();
        int thisYear = Integer.parseInt(dateFormat.format(date));

        int calAge = thisYear - yearDOB;

        if (thisMonth < monthDOB) {
            calAge = calAge - 1; 
        }

        if (thisMonth == monthDOB && thisDay < dayDOB) {
            calAge = calAge - 1;
        }

        String age = Integer.toString(calAge);

Zaktualizuj w formularzu

<tr>
    <td><label for="inputName">Birthdate</label>
       <input type="text" placeholder="mm/dd/yyyy" id="input_date" name="birthdate" maxlength="10"  />
    </td>
</tr>

Aktualizacja w źródle

<script src="../scripts/formatter.js"></script>
<script src="../scripts/formatter.min.js"></script>
<script src="../scripts/jquery.formatter.js"></script>
<script src="../scripts/jquery.formatter.min.js"></script>

Dodano skrypt

<script>
     $('#input_date').formatter({
       'pattern': '{{99}}/{{99}}/{{9999}}',
       'persistent': true
     });
</script>

Próbowałem też javascript, ale to nie działa ...

questionAnswers(4)

yourAnswerToTheQuestion