Mobile Phone number format check

Source: Internet
Author: User
Tags findone

  1. Router.post ('/register ',function (req,res,next) {
  2. var restresult = new Restresult ();
  3. var mobile = Req.body.mobile;
  4. if (!/1\d{10}/.test (mobile)) {//mobile number format check
  5. Restresult.errorcode = Restresult.illegal_argument_error_code;
  6. Restresult.errorreason = "Please fill in the true phone format";
  7. Res.send (Restresult);
  8. return;
  9. }
  10. var password = Req.body.password;
  11. if (!password | | Password.length < 6) {//password length check
  12. Restresult.errorcode = Restresult.illegal_argument_error_code;
  13. Restresult.errorreason = "Password length must not be less than 6 bits";
  14. Res.send (Restresult);
  15. return;
  16. }
  17. //findone method, the first parameter number condition, the second parameter is the field projection, the third parameter is the callback function
  18. Userentity.findone ({mobile:mobile},' _id ',function (err,user) {
  19. if (err) {//query exception
  20. Restresult.errorcode = Restresult.server_exception_error_code;
  21. Restresult.errorreason = "Server Exception";
  22. Res.send (Restresult);
  23. return;
  24. }
  25. if (user) {//Phone number registered
  26. Restresult.errorcode = Restresult.business_error_code;
  27. Restresult.errorreason = "Mobile phone number is registered";
  28. Res.send (Restresult);
  29. return;
  30. }
  31. var registeruser = New Userentity ({Mobile:mobile,password:password});
  32. //How to save an instance of a calling entity
  33. Registeruser.save (function (err,row) {
  34. if (err) {//server Save exception
  35. Restresult.errorcode = Restresult.server_exception_error_code;
  36. Restresult.errorreason = "Server Exception";
  37. Res.send (Restresult);
  38. return;
  39. }
  40. Res.send (Restresult); //Return successful results
  41. });
  42. });
  43. });
  44. Landing route
  45. Router.post ('/login ',function (req,res,next) {
  46. var restresult = new Restresult ();
  47. var mobile = Req.body.mobile;
  48. if (!/1\d{10}/.test (mobile)) {//mobile number format check
  49. Restresult.errorcode = Restresult.illegal_argument_error_code;
  50. Restresult.errorreason = "Please fill in the true phone format";
  51. Res.send (Restresult);
  52. return;
  53. }
  54. var password = Req.body.password;
  55. if (!password) {
  56. Restresult.errorcode = Restresult.illegal_argument_error_code;
  57. Restresult.errorreason = "Password cannot be empty";
  58. Res.send (Restresult);
  59. return;
  60. }
  61. Userentity.findone ({mobile:mobile,password:password},{password:0},function (err,user) {
  62. if (err) {
  63. Restresult.errorcode = Restresult.server_exception_error_code;
  64. Restresult.errorreason = "Server Exception";
  65. Res.send (Restresult);
  66. return;
  67. }
  68. if (!user) {
  69. Restresult.errorcode = Restresult.business_error_code;
  70. Restresult.errorreason = "User name or password error";
  71. Res.send (Restresult);
  72. return;
  73. }
  74. Restresult.returnvalue = user;
  75. Res.send (Restresult);
  76. //Update last Login time
  77. Userentity.update ({_id:user._id},{$set: {lastlogintime: new Date ()}}). exec ();
  78. });
  79. });
  80. Module.exports = router;

Restresult.js (Unified return data format) file contents are as follows:

[JavaScript]View PlainCopy
    1. var Restresult = function () {
    2. This.errorcode = Restresult.no_error;
    3. this.returnvalue = {};
    4. This.errorreason = "";
    5. };
    6. Restresult.no_error = 0; //No error
    7. Restresult.illegal_argument_error_code = 1; //Invalid parameter error
    8. Restresult.business_error_code = 2; //Business error
    9. Restresult.auth_error_code = 3; //Authentication error
    10. Restresult.server_exception_error_code = 5; //server Unknown error
    11. Restresult.target_not_exit_error_code = 6; //target does not exist error
    12. Module.exports = Restresult;

Mobile Phone number format check

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.