gpt4 book ai didi

javascript 输入验证不起作用

转载 作者:行者123 更新时间:2023-12-03 11:42:38 26 4
gpt4 key购买 nike

这个简单的代码运行良好

   var gotname= false;

while (gotname==false){

var username =prompt ("passanger ,what's yout name");
if (confirm("ar u " + username + "?")){
alert ("welcome "+ username);
gotname=true;
}

}

添加一些验证条件以确保输入不为空且仅单词之后。代码无法按我想要的方式工作按摩提醒确认("ar u "+ 用户名 + "?")没有捕获用户名值并显示值“false”

这是我的代码

var gotname= false;

while (gotname==false){

var username =prompt ("passanger ,what's yout name");
if( (username === null) || (username =! /^[A-Za-z ]+$/.test(username)) ){alert("enter valid one without numbers ! words only")}

else
{confirm("ar u " + username + "?")}

alert ("welcome "+ username);
gotname=true;


}

也尝试过

var gotname= false;

while (gotname==false){

var username =prompt ("passanger ,what's yout name");
if( username === null && username =! /^[A-Za-z ]+$/.test(username) ){alert("enter valid one")}


else if (confirm("ar u " + username + "?")){
alert ("welcome "+ username);
gotname=true;
}

}

最佳答案

试试这个:

<script type="text/javascript">
function isUsernameValid(username) {
return /^[A-Za-z ]+$/.test(username);
}
var gotname= false;
while (gotname==false){
var username = prompt ("passanger ,what's yout name");
if( (username === null) || !isUsernameValid(username) ){
alert("enter valid one without numbers ! words only")
} else {
confirm("ar u " + username + "?")
alert ("welcome "+ username);
}
gotname=true;


}
</script>

关于javascript 输入验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com