0

We can perform a very basic validation of email address with JavaScript by implementing the following three rules:

1.The email address must have @ character

2.The email address must have .(dot) character

3.There must be atleast 2 characters between @ and .(dot)

6 Answers 6

5

This 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.

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;
}

Fiddle

0

``This is working...My own Creation :)Email Validation

<input type=”text” name=”email” id=”email”  />
<input type=”button” name=”btnok” onclick=”validate()” />

Java Script Function

   <script type=”text/javascript”>
function validate()
{
var str;
var t=1;
str =document.getElementById(‘email’).value;
if(document.getElementById(‘email’).value==”")
{
alert(“Empty”);

}
var res = str.split(‘@’);
if(str.split(‘@’).length!=2)
{
alert(“zero @ OR morethan one @ “);
t=0;
}
var part1=res[0];
var part2=res[1];

// part1
if(part1.length==0)
{
alert(“no content bfr @”);
t=0;
}
if(part1.split(” “).length>2)
{
alert(“Invalid:Space before @”)
t=0;
}

//chk afr @ content:  part2
var dotsplt=part2.split(‘.’);  //alert(“After @ :”+part2);
if(part2.split(“.”).length<2)
{
alert(“dot missing”);
t=0;
}
if(dotsplt[0].length==0 )
{
alert(“no content b/w @ and dot”);
t=0;
}
if(dotsplt[1].length<2 ||dotsplt[1].length>4)
{alert(“err aftr dot”);
t=0;
}

if(t==1)
alert(“woooooooooooooooooooowwwww…it is a valid email”);

}

</script>
1
  • 1
    This function return as valide email ".jorgesys@puisorcitacom" =(
    – Jorgesys
    Commented Jul 4, 2019 at 21:30
0

function validateEmail(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;
}

function assert(a, b) {
  return a == b;
}

console.log(assert(validateEmail("[email protected]"), true));
console.log(assert(validateEmail("[email protected]"), true));
console.log(assert(validateEmail("[email protected]"), true));
console.log(assert(validateEmail("[email protected]"), true));
console.log(assert(validateEmail("[email protected]"), true));
console.log(assert(validateEmail("[email protected]"), true));

console.log(assert(validateEmail("[email protected]"), false));
console.log(assert(validateEmail("[email protected]."), false));
console.log(assert(validateEmail("steve@steve"), false));
console.log(assert(validateEmail("@steve.com"), false));
console.log(assert(validateEmail("steve@"), false));
console.log(assert(validateEmail("steve"), false));
console.log(assert(validateEmail("[email protected]"), false));
console.log(assert(validateEmail("[email protected]"), false));
console.log(assert(validateEmail("[email protected]"), false));

0
let ValidateEmailAddress = (email) => {
var countAt = 0;
for (let i = 0; i < email.length; i++) {
    if (email[i] == '@')
        countAt++;
    if (!CheckAllowedString(email[i])) {
        return false;
    }
}
if (countAt > 1 || countAt == 0 || 
IsAllowedCharacter(email.charAt(0)) == false)
    return false

var emailParts = email.split('@');
if (emailParts[0].length < 1 || emailParts[1] < 4 || 
emailParts[1].lastIndexOf(".") == -1) {
    return false
}

var length = emailParts[1].length;
var lastIndex = emailParts[1].lastIndexOf(".");
if (length - lastIndex <= 2) return false;
//check for -,.,_ double accurance
for (let i = 0; i < email.length; i++) {
    if (!IsAllowedCharacter(email[i]) && !IsAllowedCharacter(email[i 
+ 1])) return false;
}
for (let i = lastIndex + 1; i < length; i++) {
    if (!IsCharacterString(emailParts[1][i])) return false;
}
return true
}
let IsAllowedCharacter = (val) => {
if (typeof val === 'undefined') return true;
if (isCharacterNumeric(val) || IsCharacterString(val)) return true;
return false
}
let isCharacterNumeric = (character) => {
return $.isNumeric(character);
}
let IsCharacterString = (character) => {
var characterArray = ["a", "b", "c", "d", "e", "f", "g", "h", "i", 
"j", "k", "l", "m",
    "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
if (characterArray.indexOf(character.toLowerCase()) != -1) return 
true;
return false
}
let CheckAllowedString = (chr) => {
if (chr == '@') {
    return true
} else if (chr == '-') {
    return true
} else if (chr == '.') {
    return true
} else if (chr == '_') {
    return true
} else if (IsAllowedCharacter(chr)) {
    return true
} else {
    return false
}
}
2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 18, 2022 at 15:30
  • The question says without a regular expression and on third line I see a regular expression ` if (!/^[a-zA-Z0-9.@_-]+$/.test(email))` Commented Oct 20, 2022 at 6:45
-2

Can you try this,

 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

if (!filter.test(email.value)) {
      alert('Please provide a valid email address');
1
  • 4
    Question pretty clearly states 'without a regular expression'. While I think regex is a simpler and cleaner solution, that's not what OP is looking for.
    – p.s.w.g
    Commented Dec 14, 2013 at 20:45
-2
var checkEmail = function(value) {

    var valid = true;

    if (value.indexOf('@') == -1) {
        valid = false;
    } else {

        var parts = value.split('@');
        var domain = parts[1];

        if (domain.indexOf('.') == -1) {

            valid = false;

        } else {

            var domainParts = domain.split('.');
            var ext = domainParts[1];

            if (ext.length > 4 || ext.length < 2) {

                valid = false;
            }
        }

    }


    return valid;

};

var form = document.getElementById('test');

var validate = function(event) {
    event.preventDefault();
    var val = document.getElementById('email').value;
    var valid = checkEmail(val);

    if (!valid) {

        alert('Not a valid e-mail address');
    } else {

        alert('Valid e-mail address');

    }
};

form.addEventListener('submit', validate, false);

There are many techniques of validating email address, each validation method has its own pros and cons. The above method doesn't require understanding of regular expressions

2
  • This would also validate @xx. which is pretty obviously not an email address.
    – p.s.w.g
    Commented Dec 14, 2013 at 20:43
  • pswg,there could be a domain name coming in future with 'xx', but still i believe its valid.
    – cpp-coder
    Commented Dec 14, 2013 at 21:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.