I want to add a javascript that checks and prevents my form from submitting itself whenever an email address that has been used by someone else is provided by another user. I know this is not a professional way to do this but I need this solution for a simple project.
Below is the javascript that I have tried myself but not working.
function check(form) /*function to check used email addresses*/ {
/*the following code checkes whether the entered email address is used*/
if (form.usercheck1.value == "sttf@gmail.com"
|| form.usercheck1.value == "dandy@gmail.com")
{
alert("The email address you provided has been used by another user."));
return false;
}
return true;
}
<form action='' method='POST' onsubmit="return check_usercheck1();"></form>
<input name='email' id="usercheck1" placeholder='email' required='' type='email'/>
Your code has various errors.
check_usercheck1 function doesn't exist.), this give a error.Check the snippet below.
function check(form) {
let emails = ['sttf@gmail.com', 'dandy@gmail.com'];
if (emails.includes(form)) {
alert("The email address you provided has been used by another user.");
return false;
}
return true;
}
function even (e) {
const formInp = document.getElementById("usercheck1");
const success = check(formInp.value);
if(!success) {
e.preventDefault();
}
}
window.onload = (event) => {
document.getElementById("form").addEventListener("submit", even);
}
<form action='' method='POST' id="form">
<input name='email' id="usercheck1" placeholder='email' required='' type='email'/>
<button type="submit">Submit</button>
</form>