Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  47] [ 2]  / answers: 1 / hits: 26596  / 11 Years ago, thu, october 31, 2013, 12:00:00
<tr>
<td><label>Birthdate</label>
<input type=text placeholder=mm/dd/yyyy name=birthdate maxlength=10/>
</td>
</tr>


Well, my code is working but I want my input type text to auto format like a date (html 5 input type=date) because in my Servlet I convert it to Age.
The problem is that, if I use the input type=date the conversion is error so I decided to use input type=text and it's working. So is it possible to auto put / in this format mm/dd/yyyy? For example, if the user input 2 character an / will auto input etc.






Servlet for birthdate to Age



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





Update in the form



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


Update in the source



<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>


Added Script



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


I also tried the javascript but it's not working...


More From » jquery

 Answers
28

use datepicker api from jquery
here is the link Datepicker



and here is the working code



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

<script>
$(function() {
$( #birthdate ).datepicker();
});
</script>


EDIT



$(input[name='birthdate']:first).keyup(function(e){
var key=String.fromCharCode(e.keyCode);
if(!(key>=0&&key<=9))$(this).val($(this).val().substr(0,$(this).val().length-1));
var value=$(this).val();
if(value.length==2||value.length==5)$(this).val($(this).val()+'/');
});


this is the code that you may need



here is the fiddled code


[#74583] Wednesday, October 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;