js验证表单验证,包括用户名、密码、确认密码等,用户注册页面实例

js验证表单验证,包括用户名、密码、确认密码等,用户注册页面实例

 

本文是js验证表单输入的入门级教程,要求你会基础的HTML,JS知识。

页面HTML 表单代码:

<form action="" method="post" enctype="multipart/form-data" name="reg_form">
<div class="info_input">
    <div class="ptag">用户名:</div>
    <div class="ftag"><input name="userName" type="text" class="inputstyle" maxlength="80"/></div>
</div>
<div class="info_input">
    <div class="ptag">密码:</div>
    <div class="ftag"><input name="userPw" type="password" class="inputstyle" maxlength="64"/></div>
</div>
<div class="info_input">
    <div class="ptag">确认密码:</div>
    <div class="ftag"><input name="confirmPw" type="password" class="inputstyle" maxlength="64"/></div>
</div>
<div class="info_input">
    <div class="ptag">性别:</div>
    <div class="ftag">
    <input type="radio" name="gender" id="radio" value="male" />男
    <input type="radio" name="gender" id="radio" value="female" />女
    <input name="gender" type="radio" id="radio" value="secrecy" checked="checked" />保密
</div>
</div>
<div class="info_input">
    <div class="ptag">所在专业:</div>
    <div class="ftag">
    <select name="select" id="select">
        <option value="001">软件工程</option>
        <option value="002" selected="selected">人工智能</option>
        <option value="003">网络工程</option>
        <option value="004">物联网</option>
        <option value="005">大数据</option>
    </select>
</div>
</div>
<div class="info_input">
    <div class="ptag">爱好:</div>
<div class="ftag">
  <input name="checkbox" type="checkbox" id="checkbox" checked="checked" />读书
  <input type="checkbox" name="checkbox" id="checkbox" />旅游
  <input type="checkbox" name="checkbox" id="checkbox" />运动
  <input type="checkbox" name="checkbox" id="checkbox" />音乐
</div>
</div>
<div class="info_textarea">
  <div class="pmultitag" >个人简介:</div>
  <div class="fmultitag"><textarea name="rule" cols="50" rows="6" class="textareastyle" id="textarea"></textarea></div>
</div>
<div class="info_input">
  <div class="ptag">头像:</div>
  <div class="ftag">
  <input name="fileField" type="file" id="fileField" size="60" />
  </div>
</div>
<div class="info_btn">
  <input name="submitbtn" type="submit" class="btnstyle" id="button" value="立即注册" onClick="return checkform()" />
  <input name="resetbtn" type="reset" class="btnstyle" id="button" value="取消注册" />
</div>
</form>

表单验证的JS代码:

function checkform()
{
var userName,userPw,confirmPw;//用于保存页面上的用户名、密码及确认密码
userName=reg_form.userName.value;//取得页面上输入的用户名
userPw=reg_form.userPw.value; //取得页面上输入的密码
confirmPw=reg_form.confirmPw.value;//取得页面上输入的确认密码
if(userName==""||userPw=="") //判断用户名或密码是否为空
{
alert("用户名或密码不能为空");
return false;
}
else if(userPw!=confirmPw) //判断密码和再次输入密码是否不一致
{
alert("两次输入的密码不一直");
reg_form.userPw.focus(); //将插入点移至密码框
return false;
}
else
{ //如果一切正常,页面跳转
reg_form.action="success.html"; //页面跳转到success.html
return true;
}
}

知识点:用户输入有误,应给出错误提示,不提交表单。

上例中代码是如果有误,return false;   成功才return true;

然后将事件onClick=”return checkform()”绑定到提交按钮上。

也可以在<form>标签中通过onsubmit=”return checkform()”来实现

<form id="reg_form" name="reg_form" method="post" onsubmit="return checkform()">

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/3655.html

(2)
江山如画的头像江山如画管理团队
上一篇 2019年9月5日 上午9:25
下一篇 2019年9月10日 下午12:21

99%的人还看了以下文章

  • 36个漂亮的CSS3动态按钮,纯CSS制作无图片

    36个漂亮的纯CSS制作的动态按钮。

    2020年2月2日
    9.5K0
  • jquery实现全选、反选、取消全选功能代码

    管理系统后台,商品管理、信息管理等,需要删除,为方便操作应该提供全选、反选、取消全选功能。 今天中文网页设计分享jquery实现全选、反选、取消全选功能代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; chars…

    2018年10月17日
    9.8K0
  • 0基础学CSS

    本CSS教程能够让你在数小时内入门CSS,要求你有基本的HTML经验。

    2020年2月22日
    6.6K0
  • 如何为需要打印的网页设置单独的css样式

    项目开发:同一个页面,需要在浏览器中浏览是一种样式,而当要打印这个页面时候,又需要呈现另一种样式。 此时可以用link标签中的media属性解决此问题。 如下面这两个引用: <link href=”stylesheets/print.css” media=”print” rel=”stylesheet” type=”text/css” /> &l…

    2019年11月20日
    12.7K0
  • css布局基础:定位综合练习

    主要知识点: 1、5种常用定位:静态定位/相对定位/绝对定位/固定定位/吸附定位(磁铁定位) position:static/relative/absolute/fixed/sticky。 2、除静态定位外,都可以用left/top/bottom/right/z-index属性进行移动。 整体效果图: 效果说明: 1、随着网页向下滚动,当导航到达窗口顶端(t…

    2020年4月4日
    7.9K0
  • 跟永哥学HTML5(5):正确使用HTML5的nav和figure

    这节课就给大家讲解常见的HTML5标签错误使用写法,帮助大家理解并学会正确使用HTML5的nav和figure。 1、不要把所有列表式的链接放在nav里 随着HTML5引入了 30个新元素,我们在构造语义化和结构化的标签时的选择也变得有些不慎重。也就是说,我们不应该滥用超语义化的元素。不幸的是,nav就是这样一个被滥用的例子。 nav元素的规范描述如下: n…

    2018年2月3日
    6.0K0

发表回复

登录后才能评论