Waiting Answer November 14, 2023

Email ID Validation in JavaScript Not Working

I tried to add email ID validation in Javascript using regular expression. But the alert is not working. Is there any other method to Validate Javascript without using regular expressions? 


 
               Submit  

 

Answers
2023-11-23 02:55:34

There may be many reasons that the email ID validation in Javascript is not working. Here are a few reasons to check:

1. Logical problems in Handling errors properly
2. Incorrect regular expression that you handled
3. You are not applying the regular expression properly.

But in your code, I think your regular expression is correct. 

     const regx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

After the above code, you should validate this

    if(regx.test(email)){
        alert("Email is Valid");
    }else{
        alert("Email is Invalid");
    }

2023-11-23 03:02:38

The following is the right method to validate the Email Address. Use your logic like this,

    if(/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)){
        //success
    }else{
        //Failed
    }

2024-01-25 12:42:08

Certainly! You can validate email addresses in JavaScript without relying on regular expressions by leveraging alternative approaches such as syntax validation, DNS lookup, and SMTP verification . Here’s a simple example of how to validate an email address using JavaScript without a regular expression:

 

function testEmailAddress(emailToTest) {
  // check for @
  var atSymbol = emailToTest.indexOf("@");
  if (atSymbol < 1) return false;

  var dot = emailToTest.indexOf(".");
  if (dot <= atSymbol + 2) return false;

  // check that the dot is not at the end
  if (dot === emailToTest.length - 1) return false;

  return true;
}
 

This function satisfies all the rules you stated as well as not allowing @ to start the address and not allowing . to end the address. It does not account for multiple . in the address .

Alternatively, you can use the following JavaScript function to validate an email address without using regular expressions:

function validateEmailAddress(email) {
  var at = email.indexOf("@");
  var dot = email.lastIndexOf(".");
  return (
    email.length > 0 &&
    at > 0 &&
    dot > at + 1 &&
    dot < email.length &&
    email[at + 1] !== "." &&
    email.indexOf(" ") === -1 &&
    email.indexOf("..") === -1
  );
}
 

This function checks the validity of an email address without using regular expressions. It utilizes basic string manipulation and comparison methods to validate the email format

Your Answer