简体   繁体   中英

How to check specific email validation in phonegap android

I had created an registration form in which i had created and email i had provided validation to all the filed but I'm confused how to give validation to email field because i want that email should be either gmail.com or yahoomail.com if some one enters any other email even yahoomail.co.in it should give error message. here is the code i which checking that its having @ and . in the email or not

var atdrate=email.indexOf("@");
var dot1=email.indexOf(".");

else if(atdrate<1 || dot1<1)
    {
        alert("Enter valid Email");
        if(gml<atdrate+1)
            alert("Enter  a vaild mail id");
        else if(yml<atdrate+1)
            alert("Enter a valid email id");

    }

Using Regular Expressions is probably the best way. Here's an example ( live demo ):

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well.

*answer from https://stackoverflow.com/a/46181/1521984

Update: Then use the JavaScript split() function to split the mail address after the '@' and check the value versus your strings.

var mail = yourMailAdress.split("@");
if (mail[1] == "gmail.com" || mail[1] == "yahoomail.com") {
    // OKAY
} else {
    // false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM