来自输入的Javascript性别

时间:2014-01-20 14:46:01

标签: javascript html validation

我有一个包含输入数据的html文件和一个用于验证数据的javascript文件。

Javascript:

function alphanumeric(uadd) { 
    var letters = /^[0-9]+$/;
    if(uadd.value.match(letters))
    {
        return true;
    }
    else
    {
        alert('Only numbers');
        uadd.focus();
        return false;
    }
}

HTML:

<li><label for="isikukood">isikukood</label></li>
<li><input type="text" name="isikukood" size="50" /></li>

isikukood的长度必须是11位,不多也不少。 如果第一个“isikukood”是3gender = male4gender = female

如何将此添加到javascript?

4 个答案:

答案 0 :(得分:1)

将正则表达式字符串更改为/^[0-9]{11}$/以检查确切的11位数字,然后检查输入的第一个字符:

var input = uadd.value;
if (input.charAt(0) === '3') {
    gender = "male";
} else if (input.charAt(0) === '4') {
    gender = "female";
}

答案 1 :(得分:0)

要获得第一个字母,您可以使用charAtsubstring或正则表达式

var text = uadd.value;
var first = text.charAt(0);       //one way
var first = text.substring(0,1);  //or this way

比检查

if (first==="3") {
   alert("male");
} else if (first==="4") {
   alert("female");
} else {
   alert("error");
}

答案 2 :(得分:0)

document.getElementById('isikukood').onchange = function() {
    if (this.value.length === 11) {
        document.getElementById('gender').value = (this.value[0] === 4 ? 'Female' : 'Male');
    }
    else {
        document.getElementById('error').value = 'Incorrect length';
    }
}

在您要显示消息的位置添加2个空DIV“性别”和“错误”。

答案 3 :(得分:0)

我从你的帖子中假设你正在尝试建立爱沙尼亚社会保障号码验证。我在这里有一个我在不同的生产环境中使用过的。也许这对你有用。用法:EstonianSocialSecurityNumberValidator.validate('social_sec_no_here');

EstonianSocialSecurityNumberValidator = {

validate : function(socialSecurityNumber) {
    if(this.isEmpty(socialSecurityNumber)){
        return false;
    }
    if(!(/^[\-\+]?\d+$/).test(socialSecurityNumber)){
        return false;
    }
    if(socialSecurityNumber.length != 11){
        return false;
    }
    if (socialSecurityNumber == "10000000001" || socialSecurityNumber == "20000000002" || socialSecurityNumber == "30000000003" || socialSecurityNumber == "40000000004"
        || socialSecurityNumber == "50000000005" || socialSecurityNumber == "60000000006" || socialSecurityNumber == "70000000007" || socialSecurityNumber == "80000000008"
        || socialSecurityNumber == "90000000009") {
        return false;
    }
    var birthYear = socialSecurityNumber.substring(1,3);
    var birthMonth = socialSecurityNumber.substring(3,5);
    var birthDay = socialSecurityNumber.substring(5,7);
    var firstNr = parseInt(socialSecurityNumber.charAt(0));
    if (firstNr == 3 || firstNr == 4){
        birthYear = '19' + birthYear;
    } else {
        birthYear = '20' + birthYear;
    }
    var birthDate = birthMonth + '/' + birthDay + '/'  + birthYear;
    if(!this.isValidDate(birthDate)){
        return false;
    }

    var controlNr = parseInt(socialSecurityNumber.charAt(10));
    var otherNrs = [];
    for (var i = 0; i < 10; i++) {
        var vall = socialSecurityNumber.charAt(i);
        otherNrs.push(parseInt(vall));
    }
    var weight = (otherNrs[0] + otherNrs[1] * 2 + otherNrs[2] * 3 + otherNrs[3] * 4 + otherNrs[4] * 5 + otherNrs[5] * 6 + otherNrs[6] * 7 + otherNrs[7] * 8 + otherNrs[8] * 9 + otherNrs[9]) % 11;
    if(weight == 10){
        weight = (otherNrs[0] * 3 + otherNrs[1] * 4 + otherNrs[2] * 5 + otherNrs[3] * 6 + otherNrs[4] * 7 + otherNrs[5] * 8 + otherNrs[6] * 9 + otherNrs[7] + otherNrs[8] * 2 + otherNrs[9] * 3) % 11;
        if(weight == 10){
            weight = 0;
        }
    }
    return weight == controlNr;
},

isValidDate : function (dateStr) {
    //MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

    var matchArray = dateStr.match(datePat);
    if (matchArray == null) {
        return false;
    }
    month = matchArray[1];
    day = matchArray[3];
    year = matchArray[4];
    if (month < 1 || month > 12) {
        return false;
    }
    if (day < 1 || day > 31) {
        return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }
    if (month == 2) {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day>29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true;
},
isEmpty : function (val) {
    return val == undefined || val == '';


 }
};