基本上我試圖做一個使用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>
哪來出現的錯誤?我認爲你的正則表達式可能無效'... $/i'?應該是'... $ /,「我」)'? – DLeh 2015-04-01 20:48:42
錯誤:語法錯誤:失蹤)條件後; – 2015-04-01 20:52:07
這個'新的RegExp(/ ^((([az] | \ d | .......... FFEF])))\。?$/i)'應該是'/ ^((( [az] | \ d | .......... FFEF])))\。?$/i' – lshettyl 2015-04-01 20:53:50