从电子邮件地址修剪点

时间:2018-02-18 15:35:33

标签: javascript jquery regex validation

如何在@mail.com之前修剪任何点?我正在进行jQuery电子邮件验证,需要摆脱用户名中的所有点。

                $('document').ready(function(){
             var email_state = false;
              $('#email').on('keyup', function(){
                var email = $('#email').val();
                if (email == '') {
                    email_state = false;
                    return;
                }
                $.ajax({
          url: 'index.php',
          type: 'post',
          data: {
            'email_check' : 1,
            'email' : email,
          },
          success: function(response){.....

3 个答案:

答案 0 :(得分:2)

.replace(/\./g, "")

之前的部分使用@



function removeDots(email){
  var email_s = email.split("@");
  return email_s[0].replace(/\./g, "")+"@"+email_s[1];
}

var email = "some.emai.l@mail.com";
console.log(removeDots(email));




在您的代码的上下文中

function removeDots(email) {
  var email_s = email.split("@");
  return email_s[0].replace(/\./g, "") + "@" + email_s[1];
}

var email = "some.emai.l@mail.com";
console.log(removeDots(email));
$('document').ready(function() {
  var email_state = false;
  $('#email').on('keyup', function() {
    var email = $('#email').val();
    email = removeDots(email); // call function here to remove dots
    if (email == '') {
      email_state = false;
      return;
    }
    // Rest of your code
  });
  // Rest of your code
});

答案 1 :(得分:1)

首先使用String.prototype.split()获取username电子邮件,然后使用.replace().删除所有/\./g。以下是一个例子:



var email = "abc.d.e@mail.com";
var splitted = email.split("@");
console.log(splitted[0].replace(/\./g,"") + "@" + splitted[1]);




更新问题:

var email_state = false;
$('#email').on('keyup', function(){
  var email = $('#email').val();
  if (email == '') {
    email_state = false;
    var splitted = email.split("@");
    email = splitted[0].replace(/\./g,"") + "@" + splitted[1];
  }
}

答案 2 :(得分:1)

正则表达式\.(?![^@]+$)

一行代码:email.replace(/\.(?![^@]+$)/gy, '')



function myFunction() {
  console.clear()
  var s = document.getElementById("input").value;
  console.log(s.replace(/\.(?![^@]+$)/g, ''));
}

<form action="javascript:myFunction()">
  <input id="input" type="text" value="bla.bla.bla.@mail.net.com"><br><br>
  <input type="submit" value="Submit">
</form>
&#13;
&#13;
&#13;