gpt4 book ai didi

javascript - 将验证 js 文件链接到 html 文件错误 - Javascript

转载 作者:行者123 更新时间:2023-12-03 02:19:05 24 4
gpt4 key购买 nike

<script type="text/javascript" src = "diabetestool.js"> </script>
<meta charset="utf-8"/>

</head>

<body>
<form id = "test2" name = "test2">

<table cellpadding="2" width="20%" bgcolor="red"
align="center"
cellspacing="2"

<tr>
<td colspan =2>
<center> <font size = 4>FORM TO FILL IN </font></center>
</td>
</tr>


<td> Title </td>
<td> <select Name="Title">
<option value= "-1 selected"> select...</option>
<option value= "Mr"> Mr </option>
<option value= "Mrs"> Mrs </option>
<option value= "Miss"> Miss </option>
<option value= "Ms"> Ms </option>
<option value= "Master"> Master</option>
</select></td>
</tr>

<tr>
<td>First Name</td>
<td><input type ="text" name= "firstName" id ="firstName" size ="30"> </td>
</tr>


<tr>
<td> Last Name</td>
<td> <input type ="text" name ="lastName" id = "lastName" size ="30"> </td>
</tr>

<tr>
<td> Health Authority Number</td>
<td> <input type ="text" name ="healthNumber" id = "healthNumber" size ="30"> </td>
</tr>

<tr>
<td> Email</td>
<td> <input type ="text" name ="email" id = "email" size ="30"> </td>
</tr>

<tr>
<td> Telephone Number</td>
<td> <input type ="text" name ="telephoneNumber" id = "telephoneNumber" size ="30"> </td>
</tr>

<tr>
<td colspan ="2"> <input type="submit" value="submit form" onsubmit="return validate()"; </td>
</tr>
</table>
</form>
</body>

</html>

这是我的代码,用于创建要填写的联系表单的代码有多种选择

function validate ()
{
// Declare all the variables here
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var Title = document.getElementById("Title").value;
var healthNumber = parseInt(document,getElementById("healthNumber").value);
var email = document.getElementById("email").value;

var validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,6}$/;


if(firstName!="" && lastName!= "" && Title!="" && email !="")
{
if(email.match(validEmail))
{
alert("All Values Validated");
return true;
}
else
{
alert("Enter a valid Email");
return false;
}
}
else
{
alert("All Fields are required");
return false;
}



}

这只是一个随机创建的 js 验证代码,只是为了测试验证过程是否适用于我的代码,然后我将更改它以实际执行我想要的正确验证 问题是,提交后,我的联系表单的 html 页面只会刷新该页面,我尝试了不同的操作,但无法找到解决方案

最佳答案

您的代码有很多语法错误。我修复了语法错误。这是js fiddle :https://jsfiddle.net/0ngs96n0/13/

在表单标签中使用 onsubmit 类似的内容:

<form id = "test2" name = "test2"  onsubmit="return validate(event);">

在 JS 中:

function validate (e){
e.preventDefault();
.................
.................
.................
.................
.................
}

它将阻止表单刷新。

关于javascript - 将验证 js 文件链接到 html 文件错误 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241360/

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