Reputation: 41
I want to check the input if there is a validate emailadress. This is the function I use
function validateEmailAddress(input) {
var regex = /[^\s@]+@[^\s@]+\.[^\s@]+/;
if (regex.test(input)) {
return 1;
} else if (regex.test(input) == null || regex.test(input) == ("")) {
return 0;
} else {
return -1;
}
}
This function is used there
savePerson: function () {
$('#accountid').prop('disabled', false);
let email = $('#emailaddresses').val();
if (validateEmailAddress(email) == -1) {
alert("This email isn't validate");
return;
}
So I have to check if the input field is empty. If this is happening, the data should be validate. If the inputfield isn't an emailaddress like "dasfasf232" it has to show an alert. Only if the email is valid (like it includes an @ and a dot). At the moment I get an alert when the emailfield is empty. But that should not happen.
Upvotes: 2
Views: 5919
Reputation: 41
I solved my Problem. The line I had the Problem was not necessary. So I deleted the "else if" statement. Now it looks like this
function validateEmailAddress(input) {
var regex = /[^\s@]+@[^\s@]+\.[^\s@]+/;
if (regex.test(input)) {
return 1;
} else {
return -1;
}
}
Now in the other part I check if the email, which was typed in isn't empty and (&&) if the email is -1 (the value when the email is invalid). If one of these is true the if-statement shows an alert.
savePerson: function () {
$('#accountid').prop('disabled', false);
let email = $('#emailaddresses').val();
if (email !== "" && validateEmailAddress(email) === -1) {
alert("Use a valid emailaddress");
return;
}
Now I can save my Data in Database even though the email-inputfield is empty.
Upvotes: 2
Reputation: 820
// Try this function
function ValidateEmail(inputText)
{
var mailformat = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
if(inputText.value.match(mailformat))
{
alert("You have entered a valid email address!"); //The pop up alert for a valid email address
document.form1.text1.focus();
return true;
}
else
{
alert("You have entered an invalid email address!"); //The pop up alert for an invalid email address
document.form1.text1.focus();
return false;
}
}
Upvotes: 0