【转】注册页面的写法

前端页面代码: 

@model aiyuepai.Account_t

<link href="~/Content/Login/Register.css" rel="stylesheet" />

<div id="app">
    <div id="successful">
        <h3 style="text-align: center;margin-top: 80px;margin-bottom: 40px;">注册成功,快去登录吧!</h3>
        <button id="btn" v-on:click="close">取消</button>
        <button v-on:click="login" id="btn">登录</button>
    </div>

    <div class="now_login">我已注册,现在就<button class="btd_login" v-on:click="login">登录</button></div>

    <div id="app1">
        <form action="@Url.Action("Register")" method="post">
            <label>用户名</label><input type="text" placeholder="请设置用户名" v-model="userInfo.user" class="input" onblur="a()"><br>
            <span class="tiShi">{{tiShi.tishi1 }}</span><br>
            <label>设置密码</label><input type="password" placeholder="请设置登录密码" v-model="userInfo.password1" class="input" onblur="b()"><br>
            <span class="tiShi">{{ tiShi.tishi2 }}</span><br>
            <label>确认密码</label><input type="password" v-model="userInfo.password2" class="input" onblur="c()"><br>
            <span class="tiShi">{{ tiShi.tishi3}}</span><br>
            <label>真实姓名</label><input type="text" v-model="userInfo.name" class="input" onblur="d()"><br>
            <span class="tiShi">{{ tiShi.tishi4}}</span><br>
            <label>手机号</label><input type="text" placeholder="请输入手机号码" v-model="userInfo.num" class="input" onblur="e()"><br>
            <span class="tiShi">{{ tiShi.tishi5}}</span><br>
            <label>家庭住址</label><textarea rows="5" cols="26" placeholder="请输入您的家庭住址" v-model="userInfo.address" onblur="f()"></textarea><br>
            <span class="tiShi">{{ tiShi.tishi6}}</span><br>
            <div class="checkbox"><input type="checkbox" style="margin-right: 10px;" v-model="userInfo.checkbox">阅读并接受<a href="">《xxx用户协议》</a></div>
            <input type="text" id="test" name="test" value="美女" />
            <button type="submit" id="btd" v-on:click="flag" v-bind:disabled="btn">立即注册</button>
        </form>
    </div>
</div>


<script>
    var vm = new Vue({
        el: '#app',
        data: {
            isReturn: false,
            btn: false,
            test: 222,
            tiShi: {
                tiShi1: '',
                tishi2: '',
                tishi3: '',
                tishi4: '',
                tishi5: '',
                tishi6: '',
            },
            userInfo: {
                user: '',
                password1: '',
                password2: '',
                num: '',
                name: '',
                address: '',
                checkbox: true,
            },
            userArr: []
        },
        methods: {
            //用户名
            a() {
                this.tiShi.tishi1 = '';
                this.isReturn = true;
                var a = /^[a-z]+$/;//小写字母组成
                if (!this.userInfo.user) this.tiShi.tishi1 = '用户名不能为空';
                else if (!a.test(this.userInfo.user)) {
                    this.isReturn = true;
                    this.tiShi.tishi1 = "由小写字母组成"
                }
            },
            //密码
            b() {
                this.tiShi.tishi2 = '';
                this.isReturn = true;
                var p = /[a-zA-Z]\w[z0-9]/;
                if (!this.userInfo.password1) {
                    this.isReturn = true;
                    this.tiShi.tishi2 = '密码不能为空';
                }
                else if (!p.test(this.userInfo.password1)) {
                    this.isReturn = true;
                    this.tiShi.tishi2 = "由字母+数字组成,字母开头"
                }
            },
            //第二遍密码
            c() {
                this.tiShi.tishi3 = '';
                this.isReturn = true;
                var p = /[a-zA-Z]\w[z0-9]/;
                if (!this.userInfo.password2) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = '密码不能为空';
                }
                else if (!p.test(this.userInfo.password2)) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = "由字母+数字组成,以字母开头"
                }
                if (this.userInfo.password1 != this.userInfo.password2) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = '两次密码不一致';
                }
            },
            //真实姓名
            d() {
                this.tiShi.tishi4 = '';
                this.isReturn = true;
                if (!this.userInfo.name) this.tiShi.tishi4 = '姓名不能为空'; this.isReturn = true;
            },
            //手机号
            e() {
                this.tiShi.tishi5 = '';
                this.isReturn = true;
                var n = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
                if (!this.userInfo.num) {
                    this.tiShi.tishi5 = '手机号不能为空';
                    this.isReturn = true;
                }
                else if (!n.test(this.userInfo.num)) { this.tiShi.tishi5 = '手机号码格式不正确'; this.isReturn = true; }
            },
            //家庭住址
            f() {
                this.tiShi.tishi6 = '';
                this.isReturn = true;
                if (!this.userInfo.address) { this.tiShi.tishi6 = '请输入您的家庭住址'; this.isReturn = true; }
            },
            flag() {
                //alert(1)
                this.tiShi.tishi1 = '';
                this.tiShi.tishi2 = '';
                this.tiShi.tishi3 = '';
                this.tiShi.tishi4 = '';
                this.tiShi.tishi5 = '';
                this.tiShi.tishi6 = '';
                this.isReturn = false;
                //用户名
                var a = /^[a-z]+$/;//小写字母组成
                if (!this.userInfo.user) {
                    this.tiShi.tishi1 = '用户名不能为空';
                    this.isReturn = true;
                }
                else if (!a.test(this.userInfo.user)) {
                    this.isReturn = true;
                    this.tiShi.tishi1 = "由小写字母组成"
                }
                if (window.localStorage.userArr) {
                    var array = JSON.parse(window.localStorage.userArr);
                } else { array = [] }
                //遍历数组进行匹配
                for (var i = 0; i < array.length; i++) {
                    //判断是否有相同账号
                    console.log(array)
                    /*alert(array[i].username)*/
                    if (this.userInfo.user == array[i].username) {
                        alert('该账号已存在');
                        /*this.tiShi.tiShi1='该账号已存在';*/
                        return;
                    }
                }

                //字母开头 ,由字母+数字组成
                var p = /[a-zA-Z]\w[z0-9]/;
                if (!this.userInfo.password1) {
                    this.isReturn = true;
                    this.tiShi.tishi2 = '密码不能为空';
                }
                else if (!p.test(this.userInfo.password1)) {
                    this.isReturn = true;
                    this.tiShi.tishi2 = "由字母+数字组成,字母开头"
                }
                //确认密码
                if (!this.userInfo.password2) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = '密码不能为空';
                }
                else if (!p.test(this.userInfo.password2)) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = "由字母+数字组成,以字母开头"
                }
                if (this.userInfo.password1 != this.userInfo.password2) {
                    this.isReturn = true;
                    this.tiShi.tishi3 = '两次密码不一致';
                }
                //姓名
                if (!this.userInfo.name) this.tiShi.tishi4 = '姓名不能为空'; this.isReturn = true;
                //手机号
                var n = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
                if (!this.userInfo.num) {
                    this.tiShi.tishi5 = '手机号不能为空';
                    this.isReturn = true;
                }
                else if (!n.test(this.userInfo.num)) { this.tiShi.tishi5 = '手机号码格式不正确'; this.isReturn = true; }
                //家庭住址
                if (!this.userInfo.address) { this.tiShi.tishi6 = '请输入您的家庭住址'; this.isReturn = true; }
                //同意协议
                if (this.userInfo.checkbox != true) { this.btn = false; alert('是否同意该协议'); this.isReturn = true; return }
                //如果有这些提示就return
                if (this.tiShi.tishi || this.tiShi.tishi2 || this.tiShi.tishi3 || this.tiShi.tishi4 || this.tiShi.tishi5 || this.tiShi.tishi6) return;
                //if (this.isReturn) return;
                var box = document.getElementById("successful");
                box.style.display = 'block';
                //创建对象
                var obj = { username: this.userInfo.user, password: this.userInfo.password1, number: this.userInfo.num, name: this.userInfo.name, address: this.userInfo.address, }

                array.push(obj);
                window.localStorage.userArr = JSON.stringify(array);//把内容转换成字符串形式
                /*userInfo =JSON.stringify(window.localStorage.userArr)*/
                localStorage.setItem('userArr', window.localStorage.userArr);
               
                // window.location.href='../登录/登录页面.html';
            },
            login() {
                window.location.href = "Login";
            },
            close() {
                var box = document.getElementById("successful");
                box.style.display = "none";
            }

        }
    })


</script>

前端样式:

*{
    margin: 0px;
    padding: 0px;
}

a {
    color: black;
    text-decoration: none;
}

body {
    font-family: "宋体";
    background: url(images/2.jpg) no-repeat;
    background-size: 100%;
    color: black;
}

.now_login {
    font-size: 14px;
    float: right;
}

.btd_login {
    height: 38px;
    width: 50px;
    border: 1px solid silver;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    margin-left: 10px;
}

#app1 {
    width: 300px;
    margin: 100px auto;
}

label {
    display: inline-block;
    width: 70px;
    height: 38px;
    text-align: center;
}

.tiShi {
    font-size: 12px;
    color: red;
    margin: 0 0 10px 70px;
}

.input {
    width: 200px;
    height: 38px;
    border-radius: 4px;
}

textarea {
    resize: none;
    border-radius: 4px;
}

.checkbox {
    font-size: 14px;
    margin-left: 20px;
}

#btd {
    width: 270px;
    height: 38px;
    border-radius: 5px;
    background: #5B8020;
    border: 1px solid silver;
    color: white;
    font-size: 18px;
    cursor: pointer;
    margin-top: 10px;
}

#successful {
    width: 300px;
    height: 200px;
    border: 1px solid silver;
    position: fixed;
    left: 50%;
    bottom: 50%;
    background: #D8D8D8;
    display: none;
}

#btn {
    width: 50px;
    height: 30px;
    display: inline-block;

    
    margin-right: 15px;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简单的 Vue 2 登录注册页面的完整代码展示。其中使用了 Vue Router 管理路由,使用了 Vuex 管理状态,使用了 axios 发送网络请求。 ```html <template> <div> <router-link to="/login">登录</router-link> <router-link to="/register">注册</router-link> <router-view></router-view> </div> </template> <script> import Vue from 'vue'; import VueRouter from 'vue-router'; import Vuex from 'vuex'; import axios from 'axios'; Vue.use(VueRouter); Vue.use(Vuex); const routes = [ { path: '/login', component: { template: ` <div> <h2>登录</h2> <form @submit.prevent="login"> <label> 用户名: <input v-model="username" type="text" required> </label> <label> 密码: <input v-model="password" type="password" required> </label> <button type="submit">登录</button> </form> </div> `, data() { return { username: '', password: '', }; }, methods: { async login() { try { const response = await axios.post('/api/login', { username: this.username, password: this.password, }); this.$store.commit('login', response.data); this.$router.push('/'); } catch (error) { window.alert('登录失败,请检查用户名和密码是否正确。'); } }, }, }, }, { path: '/register', component: { template: ` <div> <h2>注册</h2> <form @submit.prevent="register"> <label> 用户名: <input v-model="username" type="text" required> </label> <label> 密码: <input v-model="password" type="password" required> </label> <button type="submit">注册</button> </form> </div> `, data() { return { username: '', password: '', }; }, methods: { async register() { try { const response = await axios.post('/api/register', { username: this.username, password: this.password, }); this.$store.commit('login', response.data); this.$router.push('/'); } catch (error) { window.alert('注册失败,请检查用户名是否已经被注册。'); } }, }, }, }, ]; const router = new VueRouter({ routes, }); const store = new Vuex.Store({ state: { user: null, }, mutations: { login(state, user) { state.user = user; }, logout(state) { state.user = null; }, }, }); const app = new Vue({ router, store, }).$mount('#app'); </script> ``` 在这个例子中,我们使用了 Vue Router 来管理路由,使用了 Vuex 来管理状态。在登录和注册页面中,我们使用了 axios 发送网络请求。在登录成功和注册成功时,我们使用 Vuex 中的 mutations 来更新用户状态,并使用 Vue Router 跳到首页。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值