年龄验证Javascript

时间:2013-04-01 06:36:42

标签: javascript validation

到目前为止有这个但不会工作吗?

//if no age was entered it will allow
var age=document.getElementById('age1').value;
if(age == "")     
    return true;

//check if age is a number or less than or greater than 100
if (isNaN(age)||age<1||age>100)
{ 
    alert("The age must be a number between 1 and 100");
    return false;
}

我只需要验证!!!!

7 个答案:

答案 0 :(得分:1)

尝试使用快捷方式+转换为Number,或使用parseInt(value, 10)

var age = +document.getElementById('age1').value;

if ( !( age > 1 && age<100 ) ){

        alert("The age must be a number between 1 and 100");
        return false;
}

return true;

答案 1 :(得分:1)

为什么不使用正则表达式来验证这一点。使用以下正则表达式:

/^[1-9]?[0-9]{1}$|^100$/

此正则表达式匹配1位或2位数字,或100:

答案 2 :(得分:0)

你需要parseInt()你回到年龄的值,因为它以字符串形式出现。

答案 3 :(得分:0)

您应该使用parseInt(value, radix)将字符串转换为数字。使用此方法时提供radix是一种很好的做法。在您的情况下,它是小数,因此radix10

试试这个:

//if no age was entered it will allow
var age=document.getElementById('age1').value;
if(age === "") {
  return true;
}

// convert age to a number
age = parseInt(age, 10);

//check if age is a number or less than or greater than 100
if (isNaN(age) || age < 1 || age > 100)
{ 
  alert("The age must be a number between 1 and 100");
  return false;
}

答案 4 :(得分:0)

尝试一下

<input type='text' name='dob' onblur="Age_validator(this.value)" style='width:108px;' />
<div><span id="dob1"></span></div>
function Age_validator(str){
    // which user inputs
    str = str.split("-");
    var dd = str[0];
    var mm = str[1];
    var yy = str[2];
    // Current date calculation
    var d = new Date();
    var ds = d.getDate();
    var ms = d.getMonth();
    var ys = d.getFullYear();
    // convert 18years as days from current date
    var days = ((18 * 12) * 30) + (ms * 30) + ds;
    // convert days from input value
    var age = (((ys - yy) * 12) * 30) + ((12 - mm) * 30) + parseInt(30 - dd);

    if((days - age) <= '0'){
        console.log((days - age));
        document.getElementById('dob1').innerHTML = 'Valid';
        // or return your own script
    }else{
        console.log((days - age));
        document.getElementById('dob1').innerHTML = 'Invalid';
        // or return your own script
    }
}

如果有用,请+1

答案 5 :(得分:0)

从我这里尝试这个方法:)

var dateString = document.getElementById("age1").value;
if(dateString !=""){
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  var da = today.getDate() - birthDate.getDate();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age--;
  }
  if(m<0){
    m +=12;
  }
  if(da<0){
    da +=30;
  }
  if (age >= 1 && age <= 99) {
    return true;
  }else {
    return false;
  }

}else {
  return false;
}

您还可以使用其他库来计算诸如时刻https://momentjs.com/之类的时间,从而可以更轻松地计算时间(秒,分钟,小时,天,年等)。

答案 6 :(得分:0)

尝试一下,最小年龄18岁,最大70岁

validateAge(year,month,day) {

    const max_year = new Date().getFullYear() - 70 ;
    const min_year = new Date().getFullYear() - 18 ;
    const _month = new Date().getMonth() + 1;
    const _day = new Date().getDay();

    const dateofbirthDate = new Date(year + "-"+month+"-"+day);
    const mindate = new Date( min_year+ '-'+_month+'-'+_day);
    const maxdate = new Date(max_year+ '-'+_month+'-'+_day);


    if(dateofbirthDate <= mindate && dateofbirthDate >= maxdate){
        return true;
    }
    else
        return false; 
}