最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Validating email address using jscript without a regular expression - Stack Overflow

programmeradmin2浏览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)

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)

Share asked Dec 14, 2013 at 20:41 py-coderpy-coder 3151 gold badge3 silver badges13 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 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

``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>

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."), 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));

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

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');
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

发布评论

评论列表(0)

  1. 暂无评论