Email validation using jQuery

后端 未结 30 2240
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 16:52

I\'m new to jQuery and was wondering how to use it to validate email addresses.

30条回答
  •  不知归路
    2020-11-22 17:20

    Javascript Email Validation in MVC/ASP.NET

    The problem I came across while using Fabian's answer, is implementing it in an MVC view because of the Razor @ symbol. You have to include an additional @ symbol to escape it, like so: @@

    To Avoid Razor In MVC

    function isEmail(email) {
      var regex = /^([a-zA-Z0-9_.+-])+\@@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      return regex.test(email);
    }
    

    I didn't see it elsewhere on this page, so I thought it might be helpful.

    EDIT

    Here's a link from Microsoft describing it's usage.
    I just tested the code above and got the following js:

    function validateEmail(email) {
      var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
      return regex.test(email);
    }
    

    Which is doing exactly what it's supposed to do.

提交回复
热议问题