I have created a simple form in HTML that submits data to Microsoft CDS using javascript. The form submits fine.
I wanted to put in some validation to ensure that fields must be completed (no empty fields) so I used javascript form validation. I have only done validation on the first two fields: Title and First Name.
The validation works to the extent that a message pops up saying "The title must not be empty" and "The first name must not be empty" - BUT... the form still submits!
I think the problem is that I'm using two separate Scripts, one to run the validation and one to submit the data. How do I combine the two?
<!DOCTYPE html>
<html>
<head>
<title>Contact Us</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8" />
<! -- JQUERY LINK -->
<script src="jq.min.js"></script>
<style>
body {font-family: Arial, Helvetica, sans-serif;}
* {box-sizing: border-box;}
input, select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
resize: vertical;
}
button {
background-color: #04AA6D;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<script>
function validateform(){
var title=document.myform.Title.value;
var firstname=document.myform.FirstName.value;
if (title==null || title==""){
alert("Title can't be blank");
return false;
}else if (firstname==null || firstname==""){
alert("Firstname can't be blank.");
return false;
}
}
</script>
<h3>Contact Form to submit to SP List</h3>
<! -- HTML FORM BELOW -->
<div class="container">
<form name="myform" onsubmit="return validateform()">
<! -- HTML INPUT -->
<label>Title</label>
<input type="text" id="Title" placeholder="Your title...">
<label>First Name</label>
<input type="text" id="FirstName" placeholder="Your forename..">
<label>Last Name</label>
<input type="text" id="LastName" placeholder="Your surname..">
<label>Date</label><br>
<input type="date" id="Date" placeholder="Your surname..">
<label>Country</label>
<select id="Country">
<option value="England">England</option>
<option value="Wales">Wales</option>
<option value="Scotland">Scotland</option>
</select>
<label>Message</label>
<textarea id="Message" placeholder="Write something.." style="height:200px"></textarea>
<! -- HTML SUBMISSION -->
<button id="btn_submit">Submit</button>
<input id="postResult" type="text" value="Submission status: Awaiting submission" readonly>
</form>
</div>
<! -- JS BELOW -->
<script>
$(function () {
$("#btn_submit").click(function () {
var formData = {
Title: $("#Title").val(),
FirstName: $("#FirstName").val(),
LastName: $("#LastName").val(),
Message: $("#Message").val(),
Country: $("#Country").val(),
Date: $("#Date").val()
};
$.ajax({
contentType: 'application/json',
type: "POST",
url: "XXXXXXXXXXXX",
data: JSON.stringify(formData),
success: function () {
$("#postResult").val("Submission status: Success");
},
error: function () {
$("#postResult").val("Submission status: Failed");
}
});
});
});
</script>
</body>
</html>