I have Express.js server. Running in node.js. Using javascript server side language. There I have signup sipmle form, register new user and save users in mongoDb. POST method.
我有表达。js服务器。运行在node . js。使用javascript服务器端语言。在那里,我注册了sipmle表单,注册了新用户并在mongoDb中保存用户。POST方法。
<form action="/new" method="POST">Name:
<input type="text" name="name" class="name"/><br/>Phone number:
<input type="text" name="phone" class="phone"/><br/>email:
<input type="email" name="email" class="email"/><br/>Password:
<input type="password" id="p1" name="pass" class="pass"/><br/>Confirm password:
<input type="password" id="p2" name="confirm" class="confirm"/><br/>
<input type="submit" value="Submit" onclick="return validateForm()"/>
</form>
Need to create input validation(actualy it's "Password confirm" and "Email")Also need to use "regex". How I can realize this method?I created input data validation on client-side.It's works.Maybe I just need put this code in the server? Searching in google don't give me expected results... I saw many validation methods validator.js but not finde detailed code...Thank you for helping:)
需要创建输入验证(实际上它是“密码确认”和“电子邮件”)也需要使用“regex”。我如何实现这个方法?我在客户端上创建了输入数据验证。它的作品。也许我需要把这些代码放到服务器上?在谷歌中搜索不会得到预期的结果……我看到了许多验证方法。js但没有详细的代码…谢谢你的帮助:)
<script>
function validateForm (event) {
var p1 = document.getElementById('p1');
var p2 = document.getElementById('p2');
if (p1.value !== p2.value) {
alert('Password check!');
return false;
}
// check email
var email = document.getElementById('email');
// regex
var email_regexp = /[0-9a-zа-я_A-ZА-Я]+@[0-9a-zа-я_A-ZА-Я^.]+\.[a-zа-яА-ЯA-Z]{2,4}/i;
if (!email_regexp.test(email.value)) {
alert('Check email');
return false;
}
}
</script>
also here is my registration server side code:
这是我的注册服务器端代码:
app.use(bodyParser());
mongoose.connect('mongodb://localhost/test');
var Schema = new mongoose.Schema({
name : String,
phone: String,
email : String,
pass : String,
confirm : String
});
var user = mongoose.model('emp', Schema);
app.post('/new', function(req, res){
new user({
name : req.body.name,
phone: req.body.phone,
email: req.body.email,
pass: req.body.pass,
confirm: req.body.confirm
}).save(function(err, doc){
console.log(user);
if(err) res.json(err);
else res.send('Successfully inserted!');
});
});
1 个解决方案
#1
4
For validating email you should use-
为验证电子邮件,你应该使用-
req.checkBody('email').isEmail();
For the validation of Password and Confirm Password you should use-
为验证密码和确认密码你应该使用-。
req.assert('confirm', 'Password and Confirm Password should be same.').equals(req.body.pass);
var mappedErrors = req.validationErrors(true);
#1
4
For validating email you should use-
为验证电子邮件,你应该使用-
req.checkBody('email').isEmail();
For the validation of Password and Confirm Password you should use-
为验证密码和确认密码你应该使用-。
req.assert('confirm', 'Password and Confirm Password should be same.').equals(req.body.pass);
var mappedErrors = req.validationErrors(true);