-4

I'm having trouble validating email addresses with JS. Here is my code:

var regEmail = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
var result = regEmail.test('username');
alert(result);

So no matter what kind of email address I type, it always shows "false". I copied the regex expression from somewhere on stackoverflow, but I think it should actuylly work...

EDIT

$(document).ready(function() {

        $('#submitBtn').click(function() {

        //Überprüfe ob der Benutzername leer ist
        var username = $('#username').val();
        if (username == ""){
            alert ("User name cannot be empty!");
        }

        //Email validation

        var regEmail = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/i;
        var result = regEmail.test('[email protected]');
        alert(result);

        });
    });
milami
  • 39
  • 1
  • 6

1 Answers1

-2

The regex validates against A-to-z|A-to-Z|0-9 followed by @ symbol and A-to-z|A-to-Z|0-9 followed by dot and two to four letter of A-to-z|A-to-Z|0-9

You're passing only a single string without matching the regex rules.

slashsharp
  • 2,823
  • 2
  • 19
  • 26