Validating Email Addresses in Javascript

I think many people have already done some similar development tasks:

I would like to check people's email address only if it matches @ tomtom.com or @ stream.com.

I currently have two solutions:

  • Function use indexof()

    var checkTomTomEmail=eo.data.username.indexOf("@tomtom.com");
    var checkStreamEmail=eo.data.username.indexOf("@stream.com");
    
    
    if (checkTomTomEmail >0 || checkStreamEmail >0 )
    {
        //Run the login code
    }
    
    
    Else 
    {
        //Please login with your tomtom or stream email 
    }
    
  • Using Compliance

    var patt1=/@tomtom.com/gi;
    var patt2=/@stream.com/gi;
    var checkTomTomEmail=eo.data.username.match(patt1);
    var checkStreamEmail=eo.data.username.match(patt2);
    
    
    if(indexOf(checkTomTomEmail)> 1 ||indexOf (checkStreamEmail)>1)
    {
        //Login 
    }
    

I still think that I am not considering all the details yet. Any suggestions?
thank

+3
source share
2 answers

Perhaps if people are allowed to enter emails for these two addresses, you should only get a username and then let them choose @ tomtom.com or @ stream.com using radio books.

javascript-,

var emailPatt=/@(tomtom|stream).com/gi;

if(emailPatt.test(eo.data.username))
{
    //Login 
}
+3

...

var emailRegex = /^([0-9a-z])+@(tomtom|stream)\.com$/ig;
if (emailRegex.test(emailRegex)) {
    // Login
}

.match(...) - , .test(...), , - .

:

  • " " (, a@stream.com)
  • (/ - -/i )
  • . , "user@tomtom.com" , "user@tomtom.com".)

, , 3 , ..

, . :

  • "tom@tomtom.com Hello", .
  • , # 2 "." Regex, , , , "@tomtom1com", ...

Regex: http://www.regular-expressions.info/reference.html

+2

Source: https://habr.com/ru/post/1766364/


All Articles