简体   繁体   English

javascript-检查用户名的第一个字符

[英]javascript - check first char of username

English isn't my primary language,so I'll try to do my best to explain myself...I need to, using javascript, check the first letter of input username of the user,and see if it's between AZ or az . 英语不是我的主要语言,所以我会尽力向自己解释一下...我需要使用javascript检查用户输入的用户名的第一个字母,并查看它是否介于AZ或az之间。 If not it will delete the string the the user entered. 如果不是,它将删除用户输入的字符串。 Why isn't the code working? 为什么代码不起作用?

function UsernameRegisterVaildation()
{
    var username = document.getElementById("usernameRegister");
    var x = false;
    var y = false;
    if (x.CharAt(0) >= 'A' && x.CharAt(0) <= 'Z')
        x = true;

    if (y.CharAt(0) >= 'a' && y.CharAt(0) <= 'z')
        y = true;

    if (!x || !y)
    {
        alert("WRONG USERNAME");
    }
}

and here's the input: 这是输入:

        Username: <input type="text" id="usernameRegister" name="UserNameRegister" onchange="UsernameRegisterVaildation();"/>

It's charAt , not CharAt . 它是charAt ,而不是CharAt Also, you've got a reference to the element that holds the username, not the username itself - get the value property. 另外,您对包含用户名而不是用户名本身的元素的引用-获取value属性。

You may as well just use one regular expression, though: 不过,您也可以只使用一个正则表达式:

if(!/^[a-z]/i.test(username.value)) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM