0

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

//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;
}

我只需要验证!!!!

4

7 回答 7

2

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

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

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

于 2013-04-01T06:46:45.613 回答
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;
于 2013-04-01T06:40:43.510 回答
0

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

于 2013-04-01T06:38:08.297 回答
0

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

尝试这个:

//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;
}
于 2013-04-01T06:40:15.197 回答
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;
}

您还可以使用其他库来计算时间,例如 moment https://momentjs.com/可以更轻松地计算时间(秒、分钟、小时、天、年等)

于 2019-09-02T17:22:01.850 回答
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; 
}
于 2020-01-08T15:48:19.130 回答
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

于 2018-06-25T07:51:33.333 回答