javascript 如何在HTML中通过文本框验证男性/女性?

mzmfm0qo  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(147)

如何在HTML中通过文本框验证男性/女性?

JavaScript:

function myFunction(evn) {
    var x = document.getElementById("gender");
    if(x.value != "MALE" || x.value != "FEMALE")
    {
        return false;
    }
    return true;
}

HTML:

<input type="text" onkeyup="myFunction(event)" name="gender" maxlength="6" style="text-transform: uppercase" title="ENTER VALID MALE,FEMALE" autocomplete="off"/>
plicqrtu

plicqrtu1#

function myFunction(evn) { 

var x = document.getElementById("gender");
if(x.value.toUpperCase() != "MALE" && x.value.toUpperCase() != "FEMALE") {
    return false;
}

alert("Good text!");
return true;
}

同样在输入元素中,您需要一个id="gender",因为.getElementById不能只处理name属性

zvokhttg

zvokhttg2#

function myFunction(evn) {
    var x = document.getElementById("gender");
    if(x.value != "MALE" || x.value != "FEMALE")
    {
        return false;
    }
    return true;
}
function myFunction(evn) { var x = document.getElementById("gender"); if(x.value.toUpperCase() != "MALE" && x.value.toUpperCase() != "FEMALE") { return false; } alert("Good text!"); return true; }
<input type="text" onkeyup="myFunction(event)" name="gender" maxlength="6" style="text-transform: uppercase" title="ENTER VALID MALE,FEMALE" autocomplete="off"/>

相关问题