如何在jquery中动态更改`Email validation`错误消息

时间:2014-01-29 07:47:36

标签: javascript php jquery html

我需要jquery 中的电子邮件验证,以获取远程错误消息 以下是我的代码,如果它运行显示email already existed。它允许,还有特殊字符。如果输入XXXXX@mail,com显示已存在错误消息,我该如何解决此错误。请帮帮我

    $(document).ready(function() {        

// validate signup form on keyup and submit
var validator = $("#signupform").validate({
    rules: {                     
        email:
        {
            required: true,
            minlength: 15,
            maxlength: 50,
            remote:"email.php"
        }
    },
    messages: {            
        email: {
            minlength: "Email is  15 to 50 characters",
            maxlength:  "Email is  15 to 50 characters",    
            required: " Email should be required",
            remote: " Email should be already exists"           
        }
    },
             // specifying a submitHandler prevents the default submit, good for the demo       
    submitHandler: function() 
    {
        alert("submitted!---------");
        $("#signupform").submit();
    }, 
    // the errorPlacement has to take the table layout into account
    errorPlacement: function(error, element) 
            {      
        error.prependTo( element.parent().next() );
    },

    // set this class to error-labels to indicate valid fields
    success: function(label) {
        // set   as text for IE
        label.html(" ").addClass("checked");
    }
});


// propose username by combining first- and lastname
$("#username").focus(function() {
    var firstname = $("#firstname").val();
    var lastname = $("#lastname").val();
    if(firstname && lastname && !this.value) {
        this.value = firstname + "." + lastname;
    }
}); 
});

这是html页面: -

<form id="signupform" name="signupform" method="post" action="register.php" style="padding-top:70px;" >             
        <table align="center" style="height:200px;">            

              <tr>               
                <td class="field" align="left"><span style="width:150px;color:black;font-weight:bold;">Email:</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input id="email" name="email" style="height:25px;width:155px;" type="text" value="Email Address"  onfocus="if(this.value=='Email Address')this.value=''" onblur="if(this.value=='')this.value='Email Address'"/></td>
                <td class=""></td>
              </tr>     
                <tr>
                    <td style="width:200px;"><div><font size="1" color="#000000">By clicking 'Register Now' you are agreeing to our <a href="terms_con.php" target="_blank" style="color:red" > Terms and Conditions </a></font></div>  </td>
                </tr>   
            <tr align="">               
                <td class="field"  align="" colspan="2" style="padding-left:70px;">         
                <input align="left" value="" id="signupsubmit" name="signup"  type="image" src="images/nsignup.png" />                      
                </td>
            </tr>
         </table>

    </form>

这是我的PHP代码: -

 $email=$_REQUEST["email"];

    if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
      {
         $sql = "SELECT * FROM users where email='$email'";
     $result = mysql_query($sql,$conn);


        if(mysql_num_rows($result))
        {        
             echo 'false';
    }
    else
      {
          echo 'true';        
      }          
    }
  else
  { 
      echo 'false';
  } 

2 个答案:

答案 0 :(得分:1)

JSFiddle

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/);
    return pattern.test(emailAddress);
};

使用此

if(isValidEmailAddress('test@test.com')){
    alert('ok');
} else {
    alert('faild');
}

答案 1 :(得分:0)

使用此

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