用jQuery怎么制作简单的表单验证,代码是什么
Admin 2022-06-27 群英技术资讯 947 次浏览
今天这篇给大家分享的知识是“用jQuery怎么制作简单的表单验证,代码是什么”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,对此分享发大家做个参考,希望这篇“用jQuery怎么制作简单的表单验证,代码是什么”文章能帮助大家解决问题。jQuery表单验证实例 / 包含用户名、密码、住址、邮箱验证
如下图

别忘了引入jQuery框架!!!
话不多说直接先上jQuery部分代码:
<script type="text/javascript">
$(document).ready(function(){
var tip1 = "<span class='span1'>用户名不能为空!</span>";//声明发生错误时在输入框后面添加的span
var tip2 = "<span class='span2'>邮箱格式错误或不能为空!</span>";
var tip3 = "<span class='span3'>地址不能为空!</span>";
var tip4 = "<span class='span4'>密码长度不能小于五位且最多为十位 !</span>";
var condition = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;//声明判定邮箱格式的条件
$(".id").blur(function(){
if(!$(".id").val()){//判定用户名非空
$(".span1").remove();
$(".id").after(tip1);
}
else{
$(".span1").remove();
}
});
$(".email").blur(function(){
if(!condition.test($(".email").val())){//判定邮箱格式
$(".span2").remove();
$(".email").after(tip2);
}
else{
$(".span2").remove();
}
});
$(".adress").blur(function(){
if(!$(".adress").val()){//判定地址非空
$(".span3").remove();
$(".adress").after(tip3);
}
else{
$(".span3").remove();
}
});
$(".pwd").blur(function(){
if($(".pwd").val().length < 5||$(".pwd").val().length >10){//判定密码长度不能小于5位且不能大于10位
$(".span4").remove();
$(".pwd").after(tip4);
}
else{
$(".span4").remove();
}
});
$(".button").click(function(){//符合所有条件则弹出弹窗表单验证通过,如果不符合则弹出弹窗提醒用户更改
if(!$(".id").val()||!condition.test($(".email").val())||!$(".adress").val()||$(".pwd").val().length < 5||$(".pwd").val().length >10){
alert("注册信息有误,请更改个人信息");
}
else{
alert("注册成功");
}
})
})
</script>
结构和样式:
<div class="main_box">
<div class="title">
欢迎注册原魔
</div>
<div class="box">
<img alt="插图" src="./img/可莉派萌.png" class="img">
<form>
用户名:<input class="id" type="text" ><br>
邮 箱:<input class="email" type="text"><br>
地 址:<input class="adress" type="text"><br>
密 码:<input class="pwd" type="password"><br>
<button type = "button" class="button">注   册</button>
</form>
</div>
</div>
span{
color:white;
}
body{
font-family: sans-serif;
}
.main_box{
width: 100%;
height: 910px;
background-color: red;
background-image: linear-gradient(#e66465, #000000);
}
.title{
font-size: 5em;
color: white;
width:100%;
height: 100px;
text-align: center;
}
.box{
width: 1050px;
height: 310px;
margin: 150px auto 50px auto;
padding-left: 360px;
}
input{
height: 40px;
width: 200px;
border-radius: 20px;
border: solid 1px #B5B5B5;
margin: 10px;
font-size: 1.2em;
}
form{
color:white;
font-size:1.2em;
float: left;
margin-left: 50px;
}
.button{
width: 280px;
height: 40px;
background-color: #9781FD;
border-radius: 25px;
color:white;
font-size: 1.3em;
font-weight: 700;
margin-top: 10px;
}
.img{
width:310px;
height: 310px;
float: left;
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
目录根据条件判断按钮是否可以点击方法一:使用v-if 、v-else 来判断方法二:增加一个变量,判断vue判断点击当前元素需要判断你是点击了遮罩层还是form表单接下来是分别对e.currentTarget、e.target打印根据条件判断按钮是否可以点击按钮当在特定环境下不可点击,需要根据判断来控制点击事件。方法一
这篇文章主要为大家详细介绍了微信小程序实现下拉加载更多商品,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要给大家分享的是JavaScript用html5新方法操作元素类名的详解,早先JavaScript处理起来特别不方便,需要先取到class属性,然后对字符串进行处理。现在html5给所有元素增加了classList属性来操作类属性,非常方便,下面就一起来看看具体操作过程吧
这篇文章主要给大家介绍的是vue-cli 介绍与安装,vue-cli是和vue进行深度组合的工具,可以快速帮我们创建vue项目,并且把一些脚手架相关的代码给我们创建好。真正使用vue开发项目,都是用vue-cli来创建项目的,下面文章详细内容,需要的朋友可以参考一下
这篇文章主要给大家分享的是关于vue中activated的用法,下面还会介绍到keep-alive,下文有具体的代码,感兴趣的朋友可以参考学习,下面就跟随小编一起看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008