2015-04-01 62 views
-2

基本上我試圖做一個使用jQuery的表單驗證等.. 我想要使用event.preventDefault();函數阻止表單提交默認方式。jQuery preventDefault()不工作和語法錯誤

它似乎沒有工作。

我也試圖做一個if語句來檢查它是否是一個有效的電子郵件地址,但是由於某些原因它給出了語法錯誤。

我該如何解決這些問題?

<!DOCTYPE html> 
<html> 
<head> 
    <title>jQuery</title> 

    <meta charset="utf-8" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
</head> 

<style> 
    #wrapper { 
     margin: 0 auto; 
     width: 600px; 
     font-family: helvetica; 
     font-size: 1.2em; 
    } 

    input { 
     width: 300px; 
     height: 30px; 
     padding: 7px; 
     border-radius: 5px; 
     font-size: 1.2em; 
     border: 1px solid grey; 
     margin-bottom: 10px; 
    } 

    label { 
     width: 200px; 
     float: left; 
     padding-top: 10px; 
    } 

    #submitButton { 
     height: 50px; 
     margin-left: 200px; 
     width: 100px; 
    } 
</style> 

<body> 
    <div id="wrapper"> 

    <form id="validationForm"> 
     <label for="email">Email</label> 
     <input name="email" id="email" /> 

     <label for="phone">Telephone</label> 
     <input name="phone" /> 

     <label for="pass">Password</label> 
     <input name="pass" type="password" /> 

     <label for="pass">Confirm Password</label> 
     <input name="pass" type="password" /> 

     <input id="submitButton" type="submit" value="Submit" /> 
    </form> 

    </div> 

    <script type="text/javascript"> 

     $("#validationForm").submit(function(event) { 
    // this is the preventdefault that isn't working. 
      event.preventDefault(); 

      function isValidEmailAddress(emailAddress) { 
       var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); 
       return pattern.test(emailAddress); 
      } 
    // It's giving me syntax error for the '{' in the beginning of this if //statement. 
      if (!isValidEmailAddress($("#email").val()) { 
       alert("hello"); 
      } 

     }); 
    </script> 
</body> 
</html> 
+0

哪來出現的錯誤?我認爲你的正則表達式可能無效'... $/i'?應該是'... $ /,「我」)'? – DLeh 2015-04-01 20:48:42

+2

錯誤:語法錯誤:失蹤)條件後; – 2015-04-01 20:52:07

+0

這個'新的RegExp(/ ^((([az] | \ d | .......... FFEF])))\。?$/i)'應該是'/ ^((( [az] | \ d | .......... FFEF])))\。?$/i' – lshettyl 2015-04-01 20:53:50

回答

1

你錯過了這一行業的收盤)

if (!isValidEmailAddress($("#email").val()) { 

應該是:

if (!isValidEmailAddress($("#email").val())) { 

通知你有4 (只有3 )?這是問題。在編輯代碼時考慮使用IDE,它會立即發現這個問題。

編輯

此外,語法錯誤將會導致event.preventDefault()不火,所以這個修補程序將解決爲好。

+0

我覺得自己是一個完整的現在推遲...感謝您的幫助。 – Hurtox 2015-04-01 20:57:50

+0

對不起,大聲笑。另外,其他人說你的正則表達式有問題,可能也想看看。現在,我放入電子郵件字段的所有內容都是有效的...... – 2015-04-01 20:59:08

0

它看起來像你的代碼的語法需要一些修正:

<script type="text/javascript"> 
    $("#validationForm").on('submit', function(event) { 
     event.preventDefault(); 

     function isValidEmailAddress(emailAddress) { 
     var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); 
     return pattern.test(emailAddress); 
     } 

     if (!isValidEmailAddress($("#email").val())) { 
     alert("hello"); 
     } 
    }); 
</script> 

下面是DEMO