I use the following code to check if all form fields are not empty before the submit button is activated.
<script type="text/javascript">
function checkForm() {
var cansubmit = false;
$('form input[type="text"]').each(function (index, element) {
if (element.value == "") {
cansubmit = true;
}
});
document.getElementById("uploadButton").disabled = cansubmit;
}
</script>
How can I edit this code so it checks if the email field contains an email address before the submit button is activated?
CodePudding user response:
Use a combination of <input type="email">
and inputElement.validity
to check if the input is valid. Docs
CodePudding user response:
You can use /^\w ([\.-]?\w )*@\w ([\.-]?\w )*(\.\w{2,3}) $/
regex to validate email address:
if ((element.value == "" && element.type != "email") || (element.type == "email" && !/^\w ([\.-]?\w )*@\w ([\.-]?\w )*(\.\w{2,3}) $/.test(element.value))) {