我的jquery正则表达式不工作(电子邮件地址验证)

My jquery Regex won't work (e mail adress validation)

本文关键字:电子邮件地址 验证 工作 jquery 正则表达式 我的      更新时间:2023-09-26

我尝试了很多东西,但我似乎不能使它工作问题是无论我输入什么都被认为是假的,即使我尝试使用有效的电子邮件地址(例如ok@gmail.com)

这是我的代码

function validateEmail(email) {
        var re = /[^'s@]+@[^'s@]+'.[^'s@]+/;
        return re.test(email);
    }
	var email = $('input[name = pEmail]').val();  
	
	$('#nPopupSubmit').click(function () {
	        if (!validateEmail(email)) {
	            $('label[id = pEmailError]').show();
	            $('input[name = pEmail]').focus();  
	            return false; 
              } else {
                whatever
              });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<form id="popup1" method="post">
            <fieldset>    
                <input id="pEmail" type="text" placeholder="E-mail" value="E-mail" onclick="this.value=''" class="popup_input" name="pEmail" type="text" /> 
                <label id="pEmailError" style="color:#FF0000; display:none;">Error</label>
                <button type="submit" id="nPopupSubmit" name="nPopupSubmit">Go !</button>          
            </fieldset>
        </form>

你们有人知道发生了什么事吗?

你的函数不包含错误,也许你的jQuery?您的email变量应该在点击后定义,否则,email的值总是= "Email"(默认值)

$('#nPopupSubmit').click(function () {
    var email = $('#pEmail').val();  //<-- This is where you should put this
    if (!validateEmail(email)) {
        $('#pEmailError').show();
        $('#pEmail').focus();  
        return false; 
    } else {
        //whatever
    }
});

同样,您可以通过使用您已经给出的元素的id s来简化代码:)

function validateEmail(email) {
  var re = /[^'s@]+@[^'s@]+'.[^'s@]+/;
  return re.test(email);
}
	
$('#nPopupSubmit').click(function () {
  var email = $('#pEmail').val();  
  if (validateEmail(email) !== true) {
    $('#pEmailError').show();
    $('#pEmail').focus();  
    return false; 
  } else {
    //whatever
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id="popup1" method="post">
  <fieldset>    
    <input id="pEmail" type="text" placeholder="E-mail" value="E-mail" onclick="this.value=''" class="popup_input" name="pEmail" type="text" /> 
    <label id="pEmailError" style="color:#FF0000; display:none;">Error</label>
    <button type="submit" id="nPopupSubmit" name="nPopupSubmit">Go !</button>          
  </fieldset>
</form>

您当前的正则表达式无法按您想要的方式验证。

你可以试试:

function validateEmail(email) {
    var re = new RegExp("^[^''s@]+@[^''s@]+?''.[^''s@]+$", "m");
    console.log(email.match(re));
	if(email.match(re))
	{
	    return true;
    }
	else
	{
		return false;
	}
}
window.alert(validateEmail("a@b.c"));
window.alert(validateEmail("a @b.c"));
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
    <script>
	function validateEmail(email) {
        var re = new RegExp("^[^''s@]+@[^''s@]+?''.[^''s@]+$", "m");
		console.log(email.match(re));
		if(email.match(re))
		{
		    return true;
		}
		else
		{
		    return false;
		}
    }
	console.log(validateEmail("a@b.c"));
	</script>
</body>
</html>

希望有帮助。