Beginner to JavaScript and HTML5 (learned it a while ago and have not used it till today).
So, I spent some time looking online for an answer to this, but could not find what I was looking for (maybe I'm missing something). The issue I'm having is that I have created a form that asks a user for their name, ID, and email if needed. Now, in the real world, the IDs and names would be stored in a database where the user input data would be checked against the database; but because this is a class assignment, we were asked to create an array of three students to be checked against the user input data.
This my code for the HTML5 website:
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Student Transaction Form</title>
<meta charset="UTF-8"/>
<style>
p {
color: red;
font-family: Times New Roman, serif;
font-size: 160%;
}
</style>
</head>
<body>
<div style="text-align:center;">
<FORM METHOD="POST" id="the-form" action="#" >
<TABLE BORDER="1" align = "center" >
<caption style="font-size:30px">Student Transaction Form</caption>
<TR>
<TD style="font-size:18px">Student Name:</TD>
<TD>
<INPUT TYPE="TEXT" required = "required" placeholder = "Input Name" NAME="name" SIZE="25">
</TD>
<TD style="font-size:18px"><p>Required</p></TD>
</TR>
<TR>
<TD style="font-size:18px">Student Number:</TD>
<TD><INPUT TYPE="TEXT" pattern=".{8,8}" required = "required" placeholder = "Input Number" NAME="number" SIZE="25"></TD>
<TD style="font-size:18px"><p>Required</p></TD>
</TR>
<TR>
<TD style="font-size:18px">Student Email:</TD>
<TD><INPUT TYPE="email" NAME="email" SIZE="25" ></TD>
<TD style="font-size:18px">
<script type="text/javascript">
function ShowHideDiv(chkEmail)
{
var dvEmail = document.getElementById("dvEmail");
dvEmail.style.display = chkEmail.checked ? "block" : "none";
}
</script>
<div id="dvEmail" style="display: none">
<p>Required</p>
</div>
</TD>
</TR>
<TR>
<TD></TD>
<TD>
<form id = "form1">
<label for="chkEmail">
<input type="checkbox" id="chkEmail" onclick="ShowHideDiv(this)" />
Email me a transaction comfirmation
</label>
</form></TD>
</TR>
</TABLE>
<P><INPUT TYPE="SUBMIT" VALUE="Submit" NAME="B1"></P>
</FORM>
</body>
The code at the moment just makes sure the password (ID) is valid (has 8 chars) and, if the checkbox is checked, requires an email.
So, the question I have is: how to create an array with students name and ID connected to each other (meaning that if the name is correct but the ID is to a different person it would mark as incorrect) and how to run a check with the user input data?
An example would be that if the user typed Bill Smith for the name and 59683471 in the ID box, when submitting it would check to see if a Bill Smith with an ID of 59683471 exists; and if not, disallow the submission. If they do exist, then all is good and the form can be submitted.
Thank you.
Edit:
Function:
<script type ="text/javascript">
function checkall(Submit)
{
var name = document.getElementById("studentName").value;
var id = document.getElementById("studentNumber").value;
var studentArray =
[
{"Name": "Thomas Livshits", "Id": "33138463"},
{"Name": "James Maro", "Id": "33138743"},
{"Name": "Bill Smith", "Id": "33138356"},
];
studentArray.forEach(function(student){
if(id == student.Id && name == student.Name)
console.log("Found a match for student: " + student.Name);
});
}
</script>
and for the submit part:
<P><INPUT TYPE="SUBMIT" id = "Submit" VALUE="Submit" NAME="B1" onsubmit = "checkall(this); return false;"></P>