The onblur attribute is not working for form validation on an html page when I try to use a function in an external javascript file while the onclick
attribute works. So, only the result function from the JS file is working. Please help me know why. Below is my html and js file.
HTML file:
<!DOCTYPE html>
<html>
<head>
<title>Maintenance request</title>
<script type="text/javascript" src="MaintainScript.js></script>
<link rel="stylesheet" type="text/css" href="maintainStyle.css">
</head>
<body>
<div id="banner"><h1 style="color:#FFFFFF;">Housing Maintenance Request</h1></div>
<form>
<table>
<tr>
<td>Name:</td><td><input type="text" id="name" onblur="name();"><span id="msg1"><span></td>
</tr>
<tr>
<td>Phone:</td><td><input onblur="phone();" type="text" id="phone" placeholder="xxx-xxx-xxxx"><span id="msg2"><span></td>
</tr>
<tr>
<td>Student ID:</td><td><input onblur="id();" type="number" id="Student ID"><span id="msg3"><span></td>
</tr>
<tr>
<td>Email:</td><td><input onblur="email();" type="text" id="Email" placeholder="email@mail.com"><span id="msg4"><span></td>
</tr>
<tr>
<td>Type of request:</td>
<td><select>
<option>A/C</option>
<option>Door Lock</option>
<option>Mirror</option>
<option>Shower</option>
<option>Light out</option>
<option>Room change</option>
<option>Pest issue</option>
<option>Mold</option>
</select></td>
</tr>
<tr>
<td>Room/Apt/Suite:</td><td><input onblur="room();" type="text" id="room"><span id="msg3"></span></td>
</tr>
</table>
<div class="comments"><center>Describe the problem</center><br><textarea cols=145 rows=7></textarea></div>
<div class="submit"><input type="button" onclick="result();" value="SUBMIT"></div>
<span id="end"></span>
</form>
</body>
And then the JS file:
function name(){
var response = document.getElementById("name").value;
if(response.length == 0){
document.getElementById("msg1").style.color = "Red";
document.getElementById("msg1").innerHTML = " You did not provide name";
}
else{
document.getElementById("msg1").style.color = "Green";
document.getElementById("msg1").innerHTML = "<strong> Valid</strong>";
}
}
function phone(){
var response = document.getElementById("phone").value;
var pattern = /^d{3}-d{3}-d{4}$/;
if(pattern.test(response)){
document.getElementById("msg2").style.color = "Red";
document.getElementById("msg2").innerHTML = " Provide number in xxx-xxx-xxxx format";
}
else{
document.getElementById("msg2").style.color = "Green";
document.getElementById("msg2").innerHTML = "<strong> Valid</strong>";
}
}
function id(){
var response = document.getElementById("id").value;
if(response.length == 0){
document.getElementById("msg3").style.color = "Red";
document.getElementById("msg3").innerHTML = " You did not provide a seven-digit id";
}
else{
document.getElementById("msg3").style.color = "Green";
document.getElementById("msg3").innerHTML = "<strong> Valid</strong>";
}
}
function email(){
var emailInput = document.getElementById("email").value;
var emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (emailPattern.test(emailInput)){
document.getElementById("msg4").style.color = "Green";
document.getElementById("msg4").innerHTML = "<strong> Valid</strong>";
return (true);
}
else{
document.getElementById("msg4").style.color = "Red";
document.getElementById("msg4").innerHTML = " Invalid Email";
return (false);
}
}
function room(){
var response = document.getElementById("room").value;
var pattern = /^d{3}-s{1}$/;
if(patttern.test(response)){
document.getElementById("msg5").style.color = "Red";
document.getElementById("msg5").innerHTML = " You did not provide accurate room information";
return(true);
}
else{
document.getElementById("msg5").style.color = "Green";
document.getElementById("msg5").innerHTML = "<strong> Valid</strong>";
return(false);
}
}
function result(){
document.getElementById("end").innerHTML = "<center>Your request has been recorded. Please stay patient as the maintenance team prepares to work on it. Thanks.</center>";
}