//是否有效的Email
function checkEmail(str){
view_email=document.getElementById("view_email");
if (document.mytest.email.value.length==0){
           alert ("请输入邮箱地址!");
     document.mytest.email.focus(); 
     return false; 
    }else {
 //var str=document.mytest.email.value; 
  var pattern =/ ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ /; 
     if(pattern.test(str)){
  view_email.innerHTML="√";
return true;
 }else{
view_email.innerHTML="请输入正确的邮箱地址!";  

return false;   
        }
 }
return true;
}<form name="mytest"> 邮 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;箱:
            <input type="text" name="email" value="" onblur="checkEmail()"/>
            <c>*</c> &nbsp; &nbsp;<d><span id="view_email"></span></d></form>
写了如上的判断代码,输入如[email protected]邮箱地址,没有输出√,想请大家帮我看看哪里出了问题?

解决方案 »

  1.   

    var pattern =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;   //正则里面不能随便加空格
      <input type="text" name="email" value="" onblur="checkEmail(this.value)"/><!--忘记传值-->
      

  2.   

    checkEmail(str)函数你没有给它传参数,所以定义的时候去掉参数str,函数里把str前面的注释去掉就行了;
    另外那个pattern中首尾有空格,去掉,这样你试试//是否有效的Email
    function checkEmail(){
    view_email=document.getElementById("view_email");
    if (document.mytest.email.value.length==0){
      alert ("请输入邮箱地址!");
      document.mytest.email.focus();
      return false; 
      }else {
    var str=document.mytest.email.value; 
    var pattern =/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
      if(pattern.test(str)){
    view_email.innerHTML="√";
    return true;
    }else{
    view_email.innerHTML="请输入正确的邮箱地址!";return false;  
      }
    }
    return true;
    }