这里我有用于表单验证的NodeJS代码,我正在处理。我不能让它重置用户输入的值。
验证没有做任何事情,我只是想用用户提供的值重置输入表单的值。 有人请帮助我。
以下是代码:
注册路线:
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
var vm = {
title: 'Join us now, Brovis'
};
res.render('signup', vm);
});
router.post('/', function(req, res, next) {
var flagChecker= true;
if (flagChecker) {
var vm = {
title: 'Create an account',
input: req.body,
error: 'Something went wrong, Brovis'
};
delete vm.input.password;
return res.render('signup', vm);
}
res.redirect('/');
});
module.exports = router;
注册的index.hbs:
<link rel="stylesheet" href="css/silog.css">
<div class="jumbotron">
<div class="container">
<div class = "row">
<div class = "col-md-6 theForm">
<div class="form-box">
<div class="form-top">
<h2>Sign up now</h2>
<p>Join the community now:</p>
</div>
</div>
<div class="form-bottom">
<form role="form" action="" method="post" class="registration-form">
<div class="form-group">
<label class="sr-only" for="firstname">First name</label>
<input type="text" name="form-first-name" placeholder="First name" class="form-first-name form-control" id="form-first-name" required value = "{{input.firstname}}">
</div>
<div class="form-group">
<label class="sr-only" for="password">Last name</label>
<input type="text" name="form-last-name" placeholder="Last name..." class="form-last-name form-control" id="form-last-name" required value = {{input.password}}>
</div>
<button type="submit" class="btn">Sign me up!</button>
</form>
</div>
<span style = "color:red; font-weight: 600">{{error}}</span>
</div>
</div>
</div>
答案 0 :(得分:1)
嗯,您的输入名称/ ID是form-first-name
,但是因为您使用了firstname
而不是form-first-name
;用户提供的值未被重置。替换为:
<input type="text" name="form-first-name" placeholder="First name" class="form-first-name form-control" id="form-first-name" required value = "{{input.form-first-name}}">
此外,您在第二个输入字段中错过了引号。它应该是:
<input type="text" name="form-last-name" placeholder="Last name..." class="form-last-name form-control" id="form-last-name" required value = "{{input.form-last-name}}">
这样可行。
干杯。