Nodejs实现用户注册功能(2)

window.onload = function() { uname.onfocus = notNull; uname.onblur = notNull; upwd.onfocus = notNull; upwd.onblur = notNull; upwd1.onfocus = notNull; upwd1.onblur = notNull; email.onfocus = notNull; email.onblur = notNull; phone.onfocus = notNull; phone.onblur = notNull; upwd.onfocus = passw; upwd.onblur = passw; upwd1.onfocus = passw; upwd1.onblur = passw; //声明一个全局的xhr var xhr = new XMLHttpRequest(); var flag = true; //验证是否为空并且用户名是否已经存在 function notNull() { if(!uname.value) { p1.innerHTML = '用户名不能为空'; return; } else { p1.innerHTML = ''; getUname(); } if(!upwd.value) { p2.innerHTML = '密码不能为空'; return; } else { p2.innerHTML = ''; } if(!upwd1.value) { p3.innerHTML = '确认密码不能为空'; return; } else { p3.innerHTML = ''; } if(!email.value) { p4.innerHTML = '邮箱不能为空'; return; } else { p4.innerHTML = ''; } if(!phone.value) { p5.innerHTML = '手机号不能为空'; return; } else { p5.innerHTML = ''; } } //验证用户名是否已存在 function getUname() { xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; console.log(result); if(result === '1') { p1.innerHTML = '用户名已存在'; //如果用户名已存在,该按钮处于禁用状态 reg.setAttribute('disabled','true'); } else { p1.innerHTML = ''; reg.disabled = false; } } } var url = "/demo/seleUname?uname=" + uname.value; xhr.open('get', url, true); xhr.send(null); } //密码验证 function passw() { if(upwd.value != upwd1.value) { p3.innerHTML = '两次密码不一致'; } } reg.onclick = function() { //查询所有用户信息 xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; console.log(JSON.parse(result)); } } var url = "/demo/sele"; xhr.open('get', url, true); xhr.send(null); //执行注册 xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; if(flag) { getUname(); if(result === '1') { alert('success'); setTimeout(()=>{ location.href = 'http://localhost:8080/login_user.html'; }); } else { alert('error'); } } } } var str = "/demo/reg"; xhr.open('post', str, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var formdata = "uname=" + uname.value + "&upwd=" + upwd.value + "&email=" + email.value + ""; xhr.send(formdata); } }

界面展示:

Nodejs实现用户注册功能

总结

以上所述是小编给大家介绍的Nodejs实现用户注册功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c72aa5a4641cd71377d1d8ef96f76994.html