javascript程序验证用户名,其中包含八个字符,一个数字,以字母开头。我做错了什么? //验证用户名是否包含八个字符,第一部分包含一个字母,以及 //至少一个数字 //声明变量和常量 var用户名;//用户输入的用户名 变量charAny;//用户名中标识的文本字符 var anyNum=false;//用于检测用户名是否有一个数字变量 var指数;//索引循环变量 var BR=“”//打破 var ES=“”//空间 //显示所请求用户名的程序要求 document.write(“我们将开始帮助您选择用户名”+BR); document.write(“您的用户名必须至少包含8个字符,”+BR); 文档。写入(“以字母开头,至少包含1个数字字符。”+BR); 用户名=提示(“请输入您的用户名:”,ES); //检查用户名的长度 while(username.length

javascript程序验证用户名,其中包含八个字符,一个数字,以字母开头。我做错了什么? //验证用户名是否包含八个字符,第一部分包含一个字母,以及 //至少一个数字 //声明变量和常量 var用户名;//用户输入的用户名 变量charAny;//用户名中标识的文本字符 var anyNum=false;//用于检测用户名是否有一个数字变量 var指数;//索引循环变量 var BR=“”//打破 var ES=“”//空间 //显示所请求用户名的程序要求 document.write(“我们将开始帮助您选择用户名”+BR); document.write(“您的用户名必须至少包含8个字符,”+BR); 文档。写入(“以字母开头,至少包含1个数字字符。”+BR); 用户名=提示(“请输入您的用户名:”,ES); //检查用户名的长度 while(username.length,javascript,validation,passwords,Javascript,Validation,Passwords,问题是,只要有八个字符,即使没有数字,用户名仍然会进行验证。我做错了什么,它跳过了验证数字? 我已经进行了编辑以确保变量正确我很难测试您的代码,因为JSFIDLE中不允许使用document.write。通常情况下,您不想使用文档。但还是要编写。另一个技巧是,可以将Javascript字符串视为数组 下面是一个示例函数,它可以满足您的要求 <html> <body> <script type="text/javascript"> // Verify usern

问题是,只要有八个字符,即使没有数字,用户名仍然会进行验证。我做错了什么,它跳过了验证数字?
我已经进行了编辑以确保变量正确

我很难测试您的代码,因为JSFIDLE中不允许使用
document.write
。通常情况下,您不想使用
文档。但还是要编写
。另一个技巧是,可以将Javascript字符串视为数组

下面是一个示例函数,它可以满足您的要求

<html>
<body>
<script type="text/javascript">
// Verify username has eight characters, contains a letter in the first part and 
// at least one number

// Declare variables and constants
var username;           // username entered by user
var charAny;            // text character identified in username
var anyNum = false;     // digit variable used to detect whether the username has one or not
var index;              // index loop variable
var BR = "<br />";      //break
var ES = "";            //space

// Display program requirements for the username requested
document.write("We'll begin helping you select a username" + BR);
document.write("Your username must have at least 8 characters," + BR);
document.write("   start with a letter, and contain at least 1 numeric character." + BR);
username = prompt("Please enter your username: ", ES);

// Check for length of username
while (username.length < 8) {
    document.write("Your username must be at least 8 characters long." + BR);
    username = prompt("Please enter your username: ", ES);
}

// Check that first character is a letter
// Substring function has three arguments: string, starting position, and ending position
charAny = username.substr(0, 1);
while (charAny !== isLetter()) {
    document.write("The first character of your username must be a letter." + BR);
    username = prompt("Please enter your username: ", ES);
}

// Check that there's at least one digit in the username
while (anyNum !== false) {
// Check each character, set anyNum to true if a digit
    for (index = 1; index < username.substr(index, index); index++) {
        anyNum = username.substr(index, index);
        if (isNumeric(charAny)) {
            anyNum = true;
        }
    }

    // If anyNum is false there were no numerics
    if (anyNum !== true) {
        document.write("Your username must include at least 1 digit." + BR);
        username = prompt("Please enter your username: ", ES);
    }
}

// Thank the user and end the program
document.write("Thank you! Your new username is: " + username);

</script>
</body>
</html>
函数检查用户名(测试){
var good=真;
如果(!test[0].match(/[a-z]/i))good=false;//第一个字符不是字母
如果(test.length<8)good=false;//少于8个字符
if(test.match(/\d+/g)==null)good=false;//不包含数字
归还货物;
}

如果用户名包含所有非数字,则anyNum永远不会设置为true。 在“检查每个字符”循环中,为anyNum变量指定一个子字符串username。因此,如果用户名由所有非数字组成,变量将永远不会有布尔值(真/假)

此外,我有以下评论/发现:

  • 检查每个字符循环:将子字符串指定给charAny而不是anyNum
  • 检查每个字符循环:为charAny username.substr(索引,1)分配1个字符
  • 检查每个字符循环:检查username.length上的while子句
  • 用于第一个字符if的ceck,而不是while
  • 常规:还可以使用正则表达式检查用户名()的格式

//验证用户名是否包含八个字符,第一部分包含一个字母,以及
//至少一个数字
//声明变量和常量
var用户名;//用户输入的用户名
var anyNum=false;//用于检测用户名是否有一个数字变量
var指数;//索引循环变量
var BR=“
”//打破 var ES=“”//空间 //显示所请求用户名的程序要求 //document.write(“我们将开始帮助您选择用户名”+BR); //document.write(“您的用户名必须至少包含8个字符,”+BR); //文档。写入(“以字母开头,至少包含1个数字字符。”+BR); 用户名=提示(“请输入您的用户名:”,ES); //检查用户名的长度 如果(username.length<8){ document.write(“您的用户名长度必须至少为8个字符。”+BR); 用户名=提示(“请输入您的用户名:”,ES); } //检查第一个字符是否为字母 //子字符串函数有三个参数:字符串、起始位置和结束位置 charAny=username.substr(0,1); 如果(!Islander(charAny)){ document.write(“用户名的第一个字符必须是字母。”+BR); 用户名=提示(“请输入您的用户名:”,ES); } //检查每个字符,如果是数字,则将anyNum设置为true 对于(索引=1;索引-1); }
我被指示按如下方式处理问题:

var char1=myPassword.substr(0,1); if(isNaN(char1)=真) isFirstLetterACharacter将是您个人指定的布尔变量,然后设置为循环结束的标志

这就是我的p
function checkUsername(test){
    var good = true;
    if (!test[0].match(/[a-z]/i)) good = false; // first character is not a letter
    if (test.length < 8) good = false; // less than 8 characters
    if (test.match(/\d+/g) == null) good = false; //contains no numbers
    return good;
}
<html>
<body>
<script type="text/javascript">
// Verify username has eight characters, contains a letter in the first part and 
// at least one number

// Declare variables and constants
var username;           // username entered by user
var anyNum = false;     // digit variable used to detect whether the username has one or not
var index;              // index loop variable
var BR = "<br />";      //break
var ES = "";            //space

// Display program requirements for the username requested
//document.write("We'll begin helping you select a username" + BR);
//document.write("Your username must have at least 8 characters," + BR);
//document.write("   start with a letter, and contain at least 1 numeric character." + BR);
username = prompt("Please enter your username: ", ES);

// Check for length of username
if (username.length < 8) {
    document.write("Your username must be at least 8 characters long." + BR);
    username = prompt("Please enter your username: ", ES);
}

// Check that first character is a letter
// Substring function has three arguments: string, starting position, and ending position

charAny = username.substr(0, 1);
if (!isLetter(charAny)) {
    document.write("The first character of your username must be a letter." + BR);
    username = prompt("Please enter your username: ", ES);
}

// Check each character, set anyNum to true if a digit
    for (index = 1; index < username.length; index++) {
        charAny = username.substr(index, 1);
        if (isNumeric(charAny)) {
            anyNum = true;
        }
    }

    // If anyNum is false there were no numerics
    if (anyNum !== true) {
        document.write("Your username must include at least 1 digit." + BR);
        username = prompt("Please enter your username: ", ES);
    }

// Thank the user and end the program
document.write("Thank you! Your new username is: " + username);

function isLetter(character) {
return true;
}

function isNumeric(character) {
return ("01234567890".search(character) > -1);
}

</script>
</body>
</html>