微信小程序实现注册登录功能(表单校验、错误提示)


Posted in Javascript onDecember 10, 2019

口说无凭,实现效果如下

微信小程序实现注册登录功能(表单校验、错误提示)

前端 部分 

注册功能

1、wxml文件

<view>
 <view class="frm">
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
 
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        用户名
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input name="input" placeholder="请输入用户名" bindinput="username"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
 
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        手机号码
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input name="input" placeholder="请输入手机号码" bindinput="tell"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        密码
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input placeholder="设置密码" password="true" bindinput="password"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
 
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        确认密码
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input placeholder="设置密码" password="true" bindinput="rpassword"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
 
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button type="primary" bindtap="submitHandler">注册</button>
       </view>
      </view>
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button bindtap="ureg">用户登录</button>
       </view>
      </view>
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button bindtap="treg">教师注册</button>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
 </view>
</view>

2、js文件

"use strict";
 
Object.defineProperty(exports, "__esModule", {
 value: true
});
exports.default = Page({
 data: {
  name: "",
  pass: "",
  rpass: "",
  tell: "",
  isname: false,
  istell: false,
  ispass: false
 },
 treg: function treg() {
  wx.navigateTo({
   url: "./../teacher/tregdit"
  });
 },
 ureg: function ureg() {
  wx.navigateTo({
   url: "./../ulogin/ulogin"
  });
 },
 tell: function tell(e) {
  this.setData({ tell: e.detail.value });
 },
 username: function username(e) {
  this.setData({ name: e.detail.value });
 },
 password: function password(e) {
  this.setData({ pass: e.detail.value });
 },
 rpassword: function rpassword(e) {
  this.setData({ rpass: e.detail.value });
 },
 submitHandler: function submitHandler() {
  var that = this;
  if (that.data.name == "") {
   wx.showModal({
    title: "错误",
    content: "用户名不能为空"
   });
   that.isname = false;
  } else {
   that.isname = true;
  }
  if (that.data.pass != that.data.rpass || that.data.pass == "" || that.data.rpass == "") {
   wx.showModal({
    title: "错误",
    content: "两次密码输入不一致"
   });
   that.ispass = false;
  } else {
   that.ispass = true;
  }
  if (that.data.tell.length != 11) {
   wx.showModal({
    title: "错误",
    content: "手机格式有误"
   });
   that.istell = false;
  } else {
   that.istell = true;
  }
 
  if (that.istell && that.ispass && that.isname) {
   // 提交
   wx.request({
    url: "http://localhost:8080/Teacher/uregedit.action", //接口地址
    data: {
     username: that.data.name,
     password: that.data.pass,
     tell: that.data.tell
    },
    method: "get",
    header: {
     "content-type": "application/json"
    },
    success: function success(res) {
     //页面跳转
     wx.navigateTo({
      url: "./../tlogin/tlogin"
     });
     //页面跳转
    }
   });
   // 提交
  }
 }
});

3、wxss文件

.frm {
 margin-top: 200rpx;
}

登录功能

1、wxml文件

<view>
 <view class="frm">
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        用户名
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input name="input" placeholder="请输入用户名" bindinput="username"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        密码
       </view>
      </view>
      <view class="ui-col ui-col-8 ui-col-align-center align-center valign-middle" style="flex: 0 0 66.66666666666666%;">
       <view class="ui-col-content align-center valign-middle">
        <input placeholder="设置密码" password="true" bindinput="password"></input>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
  <view class="ui-row ui-row-border-top ui-row-border-bottom" style="margin-bottom: 20px;height:40px;">
   <view class="ui-col ui-col-border-right ui-col-align-center align-center valign-middle" style="flex:0 0 60px;">
    <view class="ui-col-content align-center valign-middle">
 
    </view>
   </view>
   <view class="ui-col" style="">
    <view class="ui-col-content">
     <view class="ui-row" style="height:40px;">
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button type="primary" bindtap="submitHandler">登录</button>
       </view>
      </view>
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button bindtap="ureg">用户注册</button>
       </view>
      </view>
      <view class="ui-col ui-col-4 ui-col-border-right ui-col-align-center align-center valign-middle" style="flex: 0 0 33.33333333333333%;">
       <view class="ui-col-content align-center valign-middle">
        <button bindtap="tlogin">老师登录</button>
       </view>
      </view>
     </view>
    </view>
   </view>
  </view>
 </view>
</view>

2、js文件

"use strict";
 
Object.defineProperty(exports, "__esModule", {
 value: true
});
exports.default = Page({
 data: {
  name: "",
  pass: "",
  isname: false,
  ispass: false
 },
 ureg: function ureg() {
  wx.navigateTo({
   url: "./../user/reg"
  });
 },
 tlogin: function tlogin() {
  wx.navigateTo({
   url: "./../tlogin/tlogin"
  });
 },
 username: function username(e) {
  this.setData({ name: e.detail.value });
 },
 password: function password(e) {
  this.setData({ pass: e.detail.value });
 },
 submitHandler: function submitHandler() {
  var that = this;
  if (that.data.name == "") {
   wx.showModal({
    title: "错误",
    content: "用户名不能为空"
   });
   that.isname = false;
  } else {
   that.isname = true;
  }
  if (that.data.pass == "") {
   wx.showModal({
    title: "错误",
    content: "密码不能为空"
   });
   that.ispass = false;
  } else {
   that.ispass = true;
  }
  if (that.ispass && that.isname) {
   // 提交
   wx.request({
    url: "http://localhost:8080/Teacher/ulogin.action", //接口地址
    data: {
     username: that.data.name,
     password: that.data.pass
    },
    method: "get",
    header: {
     "content-type": "application/json"
    },
    success: function success(res) {
     var info = res.data;
     if (info == "fail") {
      wx.showModal({
       title: "错误",
       content: "用户名或者密码输入不正确"
      });
     } else {
      //存储数据
      // 同步接口立即写入
      wx.setStorageSync("uname", that.data.name + "");
      wx.setStorageSync("indentity", "user");
      //页面跳转
      //页面跳转
      wx.switchTab({
       url: "/pages/center/center"
      });
      console.log("页面跳转111");
     }
    }
   });
   // 提交
  }
 }
});

3、wxss文件

.frm {
 margin-top: 200rpx;
}

我的后端是用java的ssh框架实现的

package cn.com.service;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.opensymphony.xwork2.ModelDriven;
import cn.com.bean.User;
@Repository(value = "ulogin")
@Scope("prototype")
public class Ulogin implements ModelDriven<User> {
 @Autowired
 private SessionFactory sf;
 @Autowired
 private User u;
 // 注册功能
 @Transactional
 public String regedit() {
 Session session = sf.getCurrentSession();
 session.save(u);
 return null;
 }
 
 // 登录功能
 @Transactional
 public String login() {
 Session session = sf.getCurrentSession();
 String sql = "from User where username=? and password=?";
 Query query = session.createQuery(sql);
 query.setString(0, u.getUsername());
 query.setString(1, u.getPassword());
 User uu = (User) query.uniqueResult();
 String info = null;
 if (uu != null) {
  info = "success";
 } else {
  info = "fail";
 }
 HttpServletResponse response = ServletActionContext.getResponse();
 response.setCharacterEncoding("utf-8");
 try {
  response.getWriter().write(info);
 } catch (IOException e) {
  e.printStackTrace();
 }
 return null;
 }
 
 @Override
 public User getModel() {
 // TODO Auto-generated method stub
 return u;
 }
}

总结

以上所述是小编给大家介绍的微信小程序实现注册登录功能(表单校验、错误提示),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Javascript 相关文章推荐
Javascript基础教程之数据类型 (字符串 String)
Jan 18 Javascript
JavaScript保留关键字汇总
Dec 01 Javascript
Vue实现路由跳转和嵌套
Jun 20 Javascript
bootstrap table实现双击可编辑、添加、删除行功能
Sep 27 Javascript
基于JavaScript实现一个简单的Vue
Sep 26 Javascript
基于JS实现web端录音与播放功能
Apr 17 Javascript
微信小程序版本自动更新的方法
Jun 14 Javascript
JS常用正则表达式超全集(密码强度校验,金额校验,IE版本,IPv4,IPv6校验)
Feb 03 Javascript
javascript绘制简单钟表效果
Apr 07 Javascript
Vue页面跳转传递参数及接收方式
Sep 09 Javascript
Vue+Java 通过websocket实现服务器与客户端双向通信操作
Sep 22 Javascript
如何在vue-cli中使用css-loader实现css module
Jan 07 Vue.js
微信小程序登陆注册功能的实现代码
Dec 10 #Javascript
vue excel上传预览和table内容下载到excel文件中
Dec 10 #Javascript
JS实现点餐自动选择框(案例分析)
Dec 10 #Javascript
原生js实现随机点餐效果
Dec 10 #Javascript
使用原生JS实现火锅点餐小程序(面向对象思想)
Dec 10 #Javascript
JavaScript多种滤镜算法实现代码实例
Dec 10 #Javascript
vue仿淘宝滑动验证码功能(样式模仿)
Dec 10 #Javascript
You might like
再说下636单管机
2021/03/02 无线电
PHP fgetcsv 定义和用法(附windows与linux下兼容问题)
2012/05/29 PHP
PHP中单引号与双引号的区别分析
2014/08/19 PHP
php过滤表单提交的html等危险代码
2014/11/03 PHP
WordPress中使主题支持小工具以及添加插件启用函数
2015/12/22 PHP
PHP CURL采集百度搜寻结果图片不显示问题的解决方法
2017/02/03 PHP
php设计模式之职责链模式实例分析【星际争霸游戏案例】
2020/03/27 PHP
JS实现模仿微博发布效果实例代码
2013/12/16 Javascript
原生js结合html5制作小飞龙的简易跳球
2015/03/30 Javascript
js小数运算出现多位小数如何解决
2015/10/08 Javascript
浅析JavaScript中的对象类型Object
2016/05/26 Javascript
基于d3.js实现实时刷新的折线图
2016/08/03 Javascript
微信小程序 radio单选框组件详解及实例代码
2017/01/10 Javascript
angular实现图片懒加载实例代码
2017/06/08 Javascript
基于代数方程库Algebra.js解二元一次方程功能示例
2017/06/09 Javascript
基于vue开发的在线付费课程应用过程
2018/01/25 Javascript
vue组件中使用props传递数据的实例详解
2018/04/08 Javascript
angularJs在多个控制器中共享服务数据的方法
2018/09/30 Javascript
微信小程序实现横向滚动导航栏效果
2019/12/12 Javascript
[04:16]DOTA2全国高校联赛16强抽签
2018/05/02 DOTA
python判断一个集合是否包含了另外一个集合中所有项的方法
2015/06/30 Python
python实现杨辉三角思路
2017/07/14 Python
深入了解和应用Python 装饰器 @decorator
2019/04/02 Python
PyTorch的深度学习入门教程之构建神经网络
2019/06/27 Python
python 魔法函数实例及解析
2019/09/25 Python
python系列 文件操作的代码
2019/10/06 Python
python suds访问webservice服务实现
2020/06/26 Python
Windows下PyCharm配置Anaconda环境(超详细教程)
2020/07/31 Python
英国、欧洲和全球租车服务:Avis英国
2016/08/29 全球购物
英国优质鞋类专家:Robinson’s Shoes
2017/12/08 全球购物
社会实践感言
2014/01/25 职场文书
社区健康教育工作方案
2014/06/03 职场文书
五年级学生评语大全
2014/12/26 职场文书
青年志愿者服务活动总结
2015/05/06 职场文书
Go语言基础map用法及示例详解
2021/11/17 Golang
Nginx中使用Lua脚本与图片的缩略图处理的实现
2022/03/18 Servers