我在课堂上有一个作业,需要制作一个简单的表单,其中有五个字段,必须填写三个。但我在编写代码时遇到了问题。
这是我的教授要求的: { 提交表单时,浏览器应检查以下内容: 1. 必填字段是否已填写 2. 使用正则表达式检查输入的格式是否适合电子邮件、电话和网站字段。需要检查的表单格式如下: - 电子邮件:[字母数字字符串,包括 . 和 _]@[字母数字字符串,包括 . 和 _].[字母字符串] - 电话号码:(ddd)ddd-dddd 或 ddd-ddd-dddd 等 - 网站:www.[字母数字字符串,包括 . _ -].[com 或 net 等] 如果发现任何错误,则不应提交表单,并生成相应的错误消息。 }
所有验证必须是“客户端”的,即在浏览器中使用 Javascript 进行(不在任何服务器上,也没有使用 Jquery 或任何其他编程语言,只能使用所需的 HTML)。严格禁止使用任何创作工具。
这是我现在拥有的,请帮忙看一下。以下是代码。我做错了什么?
这是我的教授要求的: { 提交表单时,浏览器应检查以下内容: 1. 必填字段是否已填写 2. 使用正则表达式检查输入的格式是否适合电子邮件、电话和网站字段。需要检查的表单格式如下: - 电子邮件:[字母数字字符串,包括 . 和 _]@[字母数字字符串,包括 . 和 _].[字母字符串] - 电话号码:(ddd)ddd-dddd 或 ddd-ddd-dddd 等 - 网站:www.[字母数字字符串,包括 . _ -].[com 或 net 等] 如果发现任何错误,则不应提交表单,并生成相应的错误消息。 }
所有验证必须是“客户端”的,即在浏览器中使用 Javascript 进行(不在任何服务器上,也没有使用 Jquery 或任何其他编程语言,只能使用所需的 HTML)。严格禁止使用任何创作工具。
这是我现在拥有的,请帮忙看一下。以下是代码。我做错了什么?
<script language = "JavaScript">
<!--
function validateForm(){
//This is to check that required fields are filled
var x = document.contact.Name.value;
var y = document.contact.Email.value;
var z = document.contact.Website.value;
var p = document.contact.Phone.value;
if(x==null || x ==""){
alert('Name must be filled out');
return false;
}
if (y ==null || y == ""){
alert('Email must be filled out');
return false;
}
if (z == null || z == ""){
alert('Website must be filled out');
return false;
}
if(!isEmail()){
alert('This is not a correct Email format');
}
if (!isTelephone()){
var errorText = document.createTextNode(
"This is not a correct Phone number format");
}
if(!isWeb()){
errorText = document.createTextNode(
"This is not the correct Website format");
}
}
function isTelephone(){
//This checks that the phone number is in the required format
return document.contact.Phone.value.match(
/^\(\d\d\d-d\d\d\-\d\d\d\d$/);
}
function isEmail(){
//This is for checking email format
return document.contact.Email.value.match(
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
}
function isWeb(){
return document.contact.Website.value.match(
/^([wW]{3}\.)?[a-zA-Z0-9\-.]{3,}\[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?$/);
}
-->
</script>
<body>
<form name="contact" action="" method="post" onSubmit = "return validateForm();">
Name: <input type="textbox" name="Name" value="" > <font color = red>*Required </font><br>
Company: <input type="textbox" name="Company" value="" > <font color = red>Optional </font><br>
Email: <input type="textbox" name="Email" value="" > <font color = red>*Required </font><br>
Telephone: <input type="textbox" name="Telephone" value="" > <font color = red>Optional </font><br>
Website: <input type="textbox" name="Website" value="" ><font color = red>*Required </font><br>
<input type="submit" value="Submit Contact Details">
<input type="reset" value="Clear"
ONCLICK="
alert('This will clear the form.');
Name.value=' '
Company.value=' '
Email.value=' '
Telephone.value=' '
Website.value=' '"><br>
</form>
</body>
</html>
onSubmit
中调用了验证,但是你的表单按钮的类型为button
,因此它们永远不会提交表单。 - Aides<input type="submit" value="Submit Contact Details">
- Rayon