gpt4 book ai didi

javascript - 使用 Javascript 验证表单数据

转载 作者:行者123 更新时间:2023-11-30 21:18:57 25 4
gpt4 key购买 nike

我昨天问了一个问题,在大家的建议下,我能够让我的 switch 语句正常工作(不错)。我还有两个问题:

switch 语句现在可以正常工作了。感谢所有的建议。不过,我有两个问题。

1.) 如果我的类(class)和部分不匹配,我会收到验证消息,告诉我我的部分无效(太棒了!),但在单击“确定”后,它仍会前进到确认消息,然后表单处理器页面。关于如何防止这种情况的任何建议?

2.) 在确认信息中,如果我点击取消,我会收到我的注册已被取消的正确信息(太棒了!),但它仍会前进到表单处理器页面...告诉我我的表单已被取消已提交。关于如何防止这种情况的任何建议?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--Document Head-->
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<!--Title Element-->
<title>Greendale Community College</title>
<!--Style Element-->
<style type="text/css">
body {
background-color: white;
}

h1 {
text-align: center;
font-family: Impact;
color: green;
}

p {
font-size: 72px;
color: green;
}
</style>
<!--Script Element-->
<script type="text/javascript">
/* <![CDATA[ */
// function to validate and create message confirm box
function submitRegistration() {
var fName = document.registration.firstName.value;
var lName = document.registration.lastName.value;
var cwid = document.registration.cwid.value;
var semester = document.registration.semester.value;
var course = document.registration.courses.value;
var section = document.registration.section.value;
var major = document.registration.needForMajor.value;
var semesterDisplay;
// To display semester info in confirm message
if (semester == "fall")
semesterDisplay = "Fall";
if (semester == "spring")
semesterDisplay = "Spring";
if (semester == "summer")
semesterDisplay = "Summer";
//To display major requirement in confirm message
var checkDisplay;
if (document.registration.needForMajor.checked == true) {
checkDisplay = "Course Needed For Major";
}
else {
checkDisplay = "";
}
//Validates first name
if (fName == "") {
window.alert("You must enter your first name!");
return false;
}
//Validates that first name is non-numeric
if (isNaN(fName) == false) {
window.alert("Your First Name must be non-numeric values!");
return false;
}
//Validates last name
if (lName == "") {
window.alert("You must enter your last name!");
return false;
}
//Validates that last name is non-numeric
if (isNaN(lName) == false) {
window.alert("Your Last Name must be non-numeric values!");
return false;
}
//Validates CWID
if (cwid == "") {
window.alert("You must enter your cwid!");
return false;
}
//Validates that CWID is numeric
if (isNaN(cwid) == true) {
window.alert("Your CWID must be numeric values!");
return false;
}
//Validates semester
var validateSemester = false;
for (var i = 0; i < 3; ++i) {
if (document.registration.semester[i].checked == true) {
validateSemester = true;
break;
}
}
if (validateSemester != true) {
window.alert("You must select a Semester!");
return false;
}
//Validates course
if (course == "") {
window.alert("You must select a Course!");
return false;
}
// Validates that the course and section are compatible
var error = true;
switch (course) {
case "CIS 100":
if (section == '100001') {
} else if (section == '100gw1') {
} else {
window.alert("You must select a valid section for CIS 100!");
error = false;
}
break;
case "CIS 120":
if (section == '120001') {
} else if (section == '120gw1') {
} else {
window.alert("You must select a valid section for CIS 120!");
error = false;
}
break;
case "CIS 220":
if (section == '220001') {
} else if (section == '220gw1') {
} else {
window.alert("You must select a valid section for CIS 220!");
error = false;
}
break;
case "CIS 299":
if (section == '299001') {
} else if (section == '299gw1') {
} else {
window.alert("You must select a valid section for CIS 299!");
error = false;
}
break;
case "CIS 302":
if (section == '302gw1') {
} else {
window.alert("You must select a valid section for CIS 302!");
error = false;
}
break;
case "CIS 304":
if (section == '304001') {
} else if (section == '304gw1') {
} else {
window.alert("You must select a valid section for CIS 304!");
error = false;
}
break;
case "CIS 321":
if (section == '321001') {
} else if (section == '321gw1') {
} else {
window.alert("You must select a valid section for CIS 321!");
error = false;
}
break;
case "CIS 322":
if (section == '322gw1') {
} else {
window.alert("You must select a valid section for CIS 322!");
error = false;
}
break;
case "CIS 325":
if (section == '325gw1') {
} else {
window.alert("You must select a valid section for CIS 325!");
error = false;
}
break;
case "CIS 330":
if (section == '330001') {
} else if (section == '330gw1') {
} else {
window.alert("You must select a valid section for CIS 330!");
error = false;
}
break;
case "CIS 332":
if (section == '332001') {
} else if (section == '332gw1') {
} else {
window.alert("You must select a valid section for CIS 332!");
error = false;
}
break;
case "CIS 341":
if (section == '341001') {
} else if (section == '341gw1') {
} else {
window.alert("You must select a valid section for CIS 341!");
error = false;
}
break;
case "CIS 343":
if (section == '34301a') {
} else if (section == '34301b') {
} else {
window.alert("You must select a valid section for CIS 343!");
error = false;
}
break;
case "CIS 352":
if (section == '352gw1') {
} else {
window.alert("You must select a valid section for CIS 352!");
error = false;
}
break;
case "CIS 354":
if (section == '354001') {
} else if (section == '354gw1') {
} else {
window.alert("You must select a valid section for CIS 354!");
error = false;
}
break;
case "CIS 401":
if (section == '401gw1') {
} else {
window.alert("You must select a valid section for CIS 401!");
error = false;
}
break;
case "CIS 419":
if (section == '419x01') {
} else {
window.alert("You must select a valid section for CIS 419!");
error = false;
}
break;
case "CIS 490":
if (section == '490001') {
} else if (section == '490gw1') {
} else {
window.alert("You must select a valid section for CIS 490!");
error = false;
}
break;
case "CIS 492":
if (section == '492gw1') {
} else {
window.alert("You must select a valid section for CIS 492!");
error = false;
}
break;
case "MAT 195":
if (section == '195001') {
} else if (section == '195w01') {
} else {
window.alert("You must select a valid section for MAT 195!");
error = false;
}
break;
case "MAT 215":
if (section == '215001') {
} else if (section == '215w01') {
} else {
window.alert("You must select a valid section for MAT 215!");
error = false;
}
break;
case "MAT 225":
if (section == '225001') {
} else if (section == '225w01') {
} else {
window.alert("You must select a valid section for MAT 225!");
error = false;
}
break;
case "MAT 281":
if (section == '281001') {
} else if (section == '281w01') {
} else {
window.alert("You must select a valid section for MAT 281!");
error = false;
}
}

//Validates section
if (section == "") {
window.alert("You must select a Section!");
return false;
}
//Confirm message
var confirmation = window.confirm("Student Name: " + fName + " " + lName + " CWID: " + cwid + " Semester: " + semesterDisplay + " Course: " + course + " Section: " + section + " " + checkDisplay);
//Ok and Cancel buttons
if (confirmation) {
window.alert("You have been registered for your course!");
}
else {
window.alert("Your registration has been canceled.");
}
}
//Reset function
function resetRegistration() {
var resetForm = window.confirm("Are you sure you want to reset the form?");
if (resetForm == true)
return true;
return false;
}
//Functions for mouseover and mouseout
function mouseOver(target) {
target.src = 'greendale_paper.png'
target.alt = 'paper'
}

function mouseOut(target) {
target.src = 'greendale.jpg'
target.alt = 'greendale'
}
/* ]]> */
</script>
</head>
<body>
<!--Heading Element-->
<h1>Greendale Community College</h1>
<!--Added a mouseover and mouseout-->
<center><img src="greendale.jpg" alt="greendale" width="560" height="315"
onmouseover="mouseOver(this)"
onmouseout="mouseOut(this)"
/></center>
<h3 align="center">Greendale Community College Orientation Video</h3>
<center><iframe width="560" height="315" src="https://www.youtube.com/embed/i3z5QO2O3cU" frameborder="0"></iframe></center>
<h2 align="center">Course Registration Page</h2>
<form action="FormProcessor.html" name="registration" method="get"
onsubmit="return submitRegistration()"
onreset="return resetRegistration()">
<h3>Student Information Form</h3>
<!--Student Information-->
First Name:<input type="text" name="firstName"/><br />
Last Name:<input type="text" name="lastName"/><br />
CWID:<input type="text" name="cwid" size="8" /><br />
<h3>Semester</h3>
<h4>(choose a semester)</h4>
<!--Radio Buttons to Choose Semester-->
<input type="radio" name="semester" value="fall" /> Fall 2018 <br />
<input type="radio" name="semester" value="spring" /> Spring 2018 <br />
<input type="radio" name="semester" value="summer" /> Summer 2018 <br />
<h3>Courses</h3>
<h4>(choose one course)</h4>
<table>
<!--Drop Down Box for Courses-->
<tr><td style="background:white;border:0">Courses:</td>
<td>
<select name="courses" size="1">
<option value=""></option>
<option value="CIS 100">CIS 100 Intro to CIS</option>
<option value="CIS 120">CIS 120 Application Prog I</option>
<option value="CIS 220">CIS 220 Application Prog II</option>
<option value="CIS 299">CIS 299 System Analysis I</option>
<option value="CIS 302">CIS 302 Visual Programming</option>
<option value="CIS 304">CIS 304 Cobol</option>
<option value="CIS 321">CIS 321 DB Mgt Sys and Design</option>
<option value="CIS 322">CIS 322 DB App Development</option>
<option value="CIS 325">CIS 225 Dec Support Systems</option>
<option value="CIS 330">CIS 330 Web Programming I</option>
<option value="CIS 332">CIS 332 Web Programming II</option>
<option value="CIS 341">CIS 341 CISCO CCNA I</option>
<option value="CIS 343">CIS 343 CISCO CCNA III</option>
<option value="CIS 352">CIS 352 Global Ethics in Comp</option>
<option value="CIS 354">CIS 354 Sys Project Mgt</option>
<option value="CIS 401">CIS 401 Concepts Enter Res Planning</option>
<option value="CIS 419">CIS 419 CIS Internship</option>
<option value="CIS 490">CIS 490 Systems Analysis II</option>
<option value="CIS 492">CIS 492 Systems Dev and Imp</option>
<option value="MAT 195">MAT 195 Discrete Math Structures</option>
<option value="MAT 215">MAT 215 Statistics</option>
<option value="MAT 225">MAT 225 Business Statistics</option>
<option value="MAT 281">MAT 281 Calculus I</option>
</select>
</td>
</tr>
</table>
<h3>Sections</h3>
<h4>(choose one section)</h4>
<table>
<tr><td style="background:white;border:0">Section Numbers:</td>
<td>
<!--Selection Box-->
<select name="section" multiple="multiple" size="5">
<option value=""></option>
<option value="100001">CIS 100 001</option>
<option value="100gw1">CIS 100 GW1</option>
<option value="120001">CIS 120 001</option>
<option value="120gw1">CIS 120 GW1</option>
<option value="220001">CIS 220 001</option>
<option value="220gw1">CIS 220 GW1</option>
<option value="299001">CIS 299 001</option>
<option value="299gw1">CIS 299 GW1</option>
<option value="302gw1">CIS 302 GW1</option>
<option value="304001">CIS 304 001</option>
<option value="304gw1">CIS 304 GW1</option>
<option value="321001">CIS 321 001</option>
<option value="321gw1">CIS 321 GW1</option>
<option value="322gw1">CIS 322 GW1</option>
<option value="325gw1">CIS 325 GW1</option>
<option value="330001">CIS 330 001</option>
<option value="330gw1">CIS 330 GW1</option>
<option value="332001">CIS 332 001</option>
<option value="332gw1">CIS 332 GW1</option>
<option value="341001">CIS 341 001</option>
<option value="341gw1">CIS 341 GW1</option>
<option value="34301a">CIS 343 01A</option>
<option value="34301b">CIS 343 01B</option>
<option value="352gw1">CIS 352 GW1</option>
<option value="354001">CIS 354 001</option>
<option value="354gw1">CIS 354 GW1</option>
<option value="401gw1">CIS 401 GW1</option>
<option value="419x01">CIS 419 X01</option>
<option value="490001">CIS 490 001</option>
<option value="490gw1">CIS 490 GW1</option>
<option value="492gw1">CIS 492 GW1</option>
<option value="195001">MAT 195 001</option>
<option value="195w01">MAT 195 W01</option>
<option value="215001">MAT 215 001</option>
<option value="215w01">MAT 215 W01</option>
<option value="225001">MAT 225 001</option>
<option value="225w01">MAT 225 W01</option>
<option value="281001">MAT 281 001</option>
<option value="281w01">MAT 281 W01</option>
</select>
</td>
</tr>
</table>
<!--Checkbox-->
<input type="checkbox" name="needForMajor" />
Check if the course is required for your major!<br />
<!--Submit and Reset Buttons Created-->
<input type="submit" name="submit" value="Submit"/><br />
<input type="reset" name="reset" value="Reset"/>
</form>
</body>
</html>

最佳答案

您似乎没有对 error = false 做任何事情。也许你的意思是 return false;

无论如何,您可以添加一个 if 语句来检查错误的值并根据它返回 false:

                break;
case "MAT 225":
if (section == '225001') {
} else if (section == '225w01') {
} else {
window.alert("You must select a valid section for MAT 225!");
error = false;
}
break;
case "MAT 281":
if (section == '281001') {
} else if (section == '281w01') {
} else {
window.alert("You must select a valid section for MAT 281!");
error = false;
}
}
if (error==false){
return false;
}

此外,您需要在警报中返回 false 以使其正确取消:

//Ok and Cancel buttons
if (confirmation) {
window.alert("You have been registered for your course!");
}
else {
window.alert("Your registration has been canceled.");
return false;
}

关于javascript - 使用 Javascript 验证表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379587/

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