gpt4 book ai didi

javascript - 表单数据验证无法正常工作

转载 作者:行者123 更新时间:2023-11-28 06:17:36 27 4
gpt4 key购买 nike

我想验证名称。

例如:如果我输入 vinod98 r vinod_ 它应该给我一条错误消息。我想在输入名称并单击参与后转到 start.js 页面。有人可以帮我做到这一点

<html>
<head>
<title>Aptitude Competition Online</title>
<link rel="stylesheet" type="text/css" href="index.css">
<script type="text/javascript">
function validate()
{
var nam = document.forms[0].name.value;
var reg = /^[a-zA-Z]*$/;
if(nam == "")
{
alert("Name should be filled out");
document.getElementById("name").focus();
return false;
}
else if (!reg.match(nam))
{
alert("Name contains only letters");
document.forms[0].name.focus();
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<div id="header1">
<font id="font1">Aptitude Quiz</font>
</div>
<div id="bgr">
<div id="emal">
<font style="position:absolute;top:16px;left:100px;font-size:20px;">Welcome to Aptitude Quiz</font><br><br><br>
<form name="form">
Name : <input type="text" name="name" id="name"><br><br>
<input name="Participate" type="button" value="Participate" onClick="validate()">
</form>
</div>
</div>
<div id="footer">
Contact Us : gmail@name.com
</div>
</body>
</html>

最佳答案

我纠正了错误。只需删除 if 语句中的 reg,然后使用测试方法即可。效果很好

关于javascript - 表单数据验证无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825794/

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