继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

jquery表单验证再提交

qq_一个人漫步_0
关注TA
已关注
手记 2
粉丝 0
获赞 29

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<title>name</title>
<script src="jquery.min.js" ></script>
<link type="text/css" rel="styleSheet" href="css/style.css"/>
</head>
<style>
.phone_k,.name_k{
position:relative;
margin:0 auto;
border: 0;
text-align: center;
border-radius: 0.5rem;
height: 0.65rem;
width: 4.5rem;
margin-bottom: 0.1rem;}
.lable_gs{
position:absolute;
border: 0;
right:0;
top:0;
text-align: left;
border-radius: 0.5rem;
height: 0.65rem;
width: 0.6rem;
line-height:0.65rem;
font-size: 0.4rem;
font-weight: bold;
}
.red{color:#F00;}
.green{color:#0aa770;}
</style>
<body>
<form action="/plus/diy.php" enctype="multipart/form-data" method="post" action="demo_form.asp">
<input type="hidden" name="action" value="post" />
<input type="hidden" name="diyid" value="1" />
<input type="hidden" name="do" value="2" />

<div class="name_k">
    <input type='text' name='name' id='name' value="" placeholder="你的姓名" required>
    <div class="lable_gs" id ="label0"></div>
</div>
<div class="phone_k">
    <input type='text' name='phone' id='phone' value="" placeholder="你的电话" required>
    <div class="lable_gs" id ="label1"></div>
</div>

<input type='file' name='photo1' id='photo1' style=" margin-top:0.1rem;" />
<input type='file' name='phpto2' id='phpto2' style='margin-top:-0.15rem' />

<input type="hidden" name="dede_fields" value="name,text;phone,text;photo1,img;phpto2,img" required=""/>
<input type="hidden" name="dede_fieldshash" value="ad7bf53345f396d8e5cc1ae9434b33a8" />

<input type="submit" name="submit" value="立即美眼" class='coolbg' />
 
<input type="reset" name="reset" value="重 置" class='coolbg' />
</form>
<script>
$(function(){

var ok1=false;
var ok2=false;
// 验证用户名
$('input[name="name"]').focus(function(){
    $("#label0").text('×').removeClass('green').addClass('red');
}).blur(function(){
    if($(this).val().length >= 2 && $(this).val().length <=12 && $(this).val()!=''){
        $("#label0").text('√').removeClass('red').addClass('green');
        ok1=true;
    }else{
        $("#label0").text('×').removeClass('green').addClass('red');
    }

});
//验证电话
$('input[name="phone"]').focus(function(){
    $("#label1").text('×').removeClass('green').addClass('red');
}).blur(function(){
    if($(this).val().search(/^1[3|4|5|7|8]\d{9}$/)){
        $("#label1").text('×').removeClass('green').addClass('red');
    }else{                  
        $("#label1").text('√').removeClass('red').addClass('green');
        ok2=true;
    }

});

//提交按钮,所有验证通过方可提交

$('input[name="submit"]').click(function(){
    if(ok1 && ok2){
        $('form').submit();
    }else{
        alert("您填写的姓名或电话不正确请重新填写!");
        return false;
    }
});

});
</script>
</body>
</html>

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP