我正在為我的網站制作一個注冊頁面。看起來像這樣
顯示代碼片段
<!DOCTYPE html>
<html lang="en" height="">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MVC | Signup</title>
</head>
<body>
<div>
<form action="/signup" method="post" target="nothing">
<h1>Sign up for Post-it</h1>
<input type="text" name="name" placeholder="Your name">
<input type="text" name="usr" placeholder="Username" >
<input type="password" name="psd" placeholder="Password">
<button type="submit">Sign up for post-it</button>
</form>
</div>
<iframe name="nothing" style="display:none"></iframe>
</body>
</html>
但是現在,您可以創建一個沒有用戶名或密碼的帳戶。我試過required屬性。它做了這樣的事情。
顯示代碼片段
<!DOCTYPE html>
<html lang="en" height="">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MVC | Signup</title>
</head>
<body>
<form action="/signup" method="post" class="w-1/2 flex flex-col" target="nothing" onsubmit="alert('form submitted')">
<h1 class="text-xl text-center mb-5">Sign up for post-it</h1>
<input type="text" name="name" placeholder="Your name" required>
<input type="text" name="usr" placeholder="Username" required>
<input type="password" name="psd" placeholder="Password" required>
<button type="submit">Sign up for post-it</button>
<iframe name="nothing" style="display: none"></iframe>
</body>
</html>
這可行,但“請填寫此欄位”看起來不太好。如果有人想出如何繞過必填欄位,那將對我的資料庫造成很大損害。那么有沒有辦法確保服務器端的表單不為空?注意:我也在使用 ejs,所以你可以發出這樣的錯誤訊息:
app.get('/error', (req, res) => {
res.render('signup', { error: 'enter a username'}); // Could also be "enter your name", or "enter your password".
});
uj5u.com熱心網友回復:
您可以在此處使用 Express Validator 是檔案
https://express-validator.github.io/docs/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497621.html
下一篇:如何洗掉物件Redis串列項?
