gpt4 book ai didi

javascript - Javascript 在验证用户名后不重定向用户

转载 作者:行者123 更新时间:2023-11-30 20:02:24 25 4
gpt4 key购买 nike

<html>
<head>
</head>
<body>

<script>
function myFun(){

var correct_way = /^[A-Za-z0-9]+$/;

var a = document.getElementById("User_Name").value;

if(a=="") {
document.getElementById("Message").innerHTML="Please enter a username.";
return false;
}
if(a.length<5){
document.getElementById("Message").innerHTML="Please enter a username with 5 or more characters";
return false;
}
if(a.match(correct_way))
document.location.href = "test1.html";
else{
document.getElementById("Message").innerHTML="Please enter a username with only letters and integers";
return false;
}


}
</script>

<form onsubmit="return myFun()">
<input type="text" id="User_Name" value=""></input>
<br></br>
<span id="Message"> </span>
<br></br>
<input type="submit" value="Submit"></input>
</form>
</body>

</html>

您好,我创建了一个用户名验证码。验证确实有效,但用户不会被重定向到“test1.html”文档。它们都在同一个文件夹中。检查验证后如何使其重定向?

最佳答案

您应该在函数 myFun 的末尾返回 true

HTML

<form action="test1.html" onsubmit="return myFun()">
<input type="text" id="User_Name" value="" />
<span id="Message"> </span>
<input type="submit" value="Submit" />
</form>

更新代码

function myFun(){
var correct_way = /^[A-Za-z0-9]+$/;

var a = document.getElementById("User_Name").value;

if(a=="") {
document.getElementById("Message").innerHTML="Please enter a username.";
return false;
}
if(a.length<5){
document.getElementById("Message").innerHTML="Please enter a username with 5 or more characters";
return false;
}
if(a.match(correct_way)){
alert("Sucessful Login, welcome to BREAKOUT!");

}
else{
document.getElementById("Message").innerHTML="Please enter a username with only letters and integers";
return false;
}

return true;
}

jsFiddle 演示 - http://jsfiddle.net/h67q09mp/

关于javascript - Javascript 在验证用户名后不重定向用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240179/

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