javascript 在jsp中输入dob后如何自动填充年龄字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16536965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to automatically populate the age field after entering dob filed in jsp.?
提问by ranjani
My jsp code is as follows:(A simple registartion from with fields)
我的jsp代码如下:(一个简单的字段注册)
<label>DOB</label></td><td><input type="text" name="dob" /> </td></tr>
<tr><td>
<label>AGE</label></td><td><input type="text" name="age" onclick =" ageCount()"/> </td></tr>
<tr><td>
<label>GENDER</label></td><td><input type="radio" name="gender" value="Male"/>Male<input
type="radio" name="gender" value="Female">Female</td></tr>
<tr><td>
I used a function in JS to calculate the age from dob! Dob is in sql Date format.
我用JS中的一个函数来计算dob的年龄!Dob 是 sql 日期格式。
<script type="text/javascript">
function ageCount(){
var date1 = new date();
var dob = document.getElementById("dob").value;
var date2 = new date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if(pattern.test(dob)){
var y1 = date1.getFullYear();
var y2 = date2.getFullYear();
var age = y1-y2;
document.write("Age :" +age);
return true;
}else{
alert("invalid date fromat.!! Please enter in (DD/MM/YYYY) format");
return false;
}
}
</script>
how do i use this so that when i enter the dob and press tab the age should be displayed in its field. Drop in your ideas and suggestions.
我如何使用它,以便当我输入 dob 并按 Tab 时,年龄应显示在其字段中。提出您的想法和建议。
回答by PSR
<input type="text" name="dob" onBlur="getAge(this.value)"/>
<input type="text" name="age" id="ageId"/>
function getAge(birthDate){
var birthDate= new Date(birthDate);
var currentDate= new Date();
var years = (otherDate.getFullYear() - birthDate.getFullYear());
if (currentDate.getMonth() < birthDate.getMonth() ||
currentDate.getMonth() == birthDate.getMonth() && currentDate.getDate() < birthDate.getDate()) {
years--;
}
$('#ageId').val(years);
} If the current month less than birth month or equal and currentdate is lessthan birth date i am decreasing years the value
如果当前月份小于出生月份或等于并且当前日期小于出生日期我正在减少年份值
回答by ranjani
Hey guys thanks for ur time working on my issue!!
嘿伙计们,感谢您花时间解决我的问题!!
I have solved it myself with a few changes here and there.
我已经自己解决了这里和那里的一些更改。
The modified code :( might help others if need be)
修改后的代码 :( 如果需要,可能会帮助其他人)
<script type="text/javascript">
function ageCount() {
var date1 = new Date();
var dob = document.getElementById("dob").value;
var date2 = new Date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
//Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getFullYear();
//getting current year
var y2 = date2.getFullYear();
//getting dob year
var age = y1 - y2;
//calculating age
document.getElementById("ageId").value = age;
doucment.getElementById("ageId").focus ();
return true;
} else {
alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
return false;
}
}
<tr>
<td><label>DOB</label></td>
<td><input type="text" name="dob" id="dob"
onblur="ageCount()" /></td>
</tr>
<tr>
<td><label>AGE</label></td>
<td><input type="text" name="age" id="ageId" /></td>
</tr>
回答by Faisal T A
try this
试试这个
<script type="text/javascript">
function ageCount() {
var today = new Date();
var date1 = document.getElementById("dob").value;
var dob = new Date(date1);
var month = dob.getMonth();
var day = dob.getDate();
var age = today.getFullYear() - dob.getFullYear();
if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day))
{
age--;
}
if(age < 0)
{
alert ("Invalid Date of Birth");
return false;
}
else
{
document.getElementById("age").value = age;
doucment.getElementById("age").focus ();
alert(age);
return true;
}
}
</script>
回答by JDGuide
You can use unBlur()
function , when you control leave the focus from text box your JavaScript method should be called.
您可以使用unBlur()
function ,当您控制从文本框中离开焦点时,您的 JavaScript 方法应该被调用。
Like this :-
像这样 :-
<input type="text" name="age" onblur="ageCount()"/>
Now inside your JavaScript you can calculate the age and display.
现在在您的 JavaScript 中,您可以计算年龄和显示。
<script type="text/javascript">
// Your ageCount() for calculate AGE and display AGE
</script>