So I managed to get a Javascript code that checks if the email is valid. So if you write aa@bb.com it's valid but if it's aa or aa@ or aa@bb. then it's not valid because it doesn't follow the pattern that I assign it to be in order to be valid. The thing is, I'm using Boostrap Form and if I click submit it accepts the email with the wrong pattern. How do I stop this? I still want to use Bootstrap because of the CSS.
$("#submit").click(function() {
var name = $("#fullname").val();
var email = $("#emailaddress").val();
if (name == '' || email == '') {
swal({
title: "Empty Fields!",
text: "Please check the missing fields",
icon: "warning",
button: "Ok, thank you",
});
} else if (email !== /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/) {
swal({
title: "Invalid Email!",
text: "Please write a valid email address",
icon: "warning",
button: "Ok, thank you",
});
} else {
swal({
title: "Good job!",
text: "You're subscriptions has been registered",
icon: "success",
button: "Awesome!",
});
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<div class="box">
<label for="fullname" class="form-tag">Name</label>
<input type="text" name="name" id="fullname" class="form-control" required/>
<label for="emailaddress" class="form-tag">Email</label>
<input type="email" name="email" id="emailaddress" class="form-control" required/>
<div class="button">
<button id="submit" type="submit" class="btn btn-light btn-lg">Submit</button>
</div>
</div>
</form>