1.我输入内容不正确时有提示,但点注册依旧能成功,怎么在输入内容不正确时点注册不能提交?
2.代码如下:
<script>
$(function verify() {
$('input').eq(0).focus(
function() {
if ($(this).val().length == 0) {
$(this).parent().next("div").text(
"长度为4-20位");
$(this).parent().next("div").css(
"color", '#ccc');
}
})
$('input').eq(1).focus(
function() {
if ($(this).val().length == 0) {
$(this).parent().next("div").text(
"长度为6-20位");
$(this).parent().next("div").css(
"color", '#ccc');
}
})
$('input').eq(0).blur(
function() {
if ($(this).val().length == 0) {
$(this).parent().next("div").text("");
$(this).parent().next("div").css(
"color", 'red');
} else if ($(this).val().length > 10
|| $(this).val().length < 4) {
$(this).parent().next("div").text(
"长度只能在4-20位");
$(this).parent().next("div").css(
"color", 'red');
} else {
$(this).parent().next("div").text("");
}
})
$('input').eq(1).blur(
function() {
if ($(this).val().length == 0) {
$(this).parent().next("div").text("");
$(this).parent().next("div").css(
"color", '#ccc');
} else if ($(this).val().length > 10
|| $(this).val().length < 6) {
$(this).parent().next("div").text(
"长度只能在6-20位");
$(this).parent().next("div").css(
"color", 'red');
} else {
$(this).parent().next("div").text("");
}
})
})
function check(e) {
for (var j = 0; j < 2; j++) {
if ($('input').eq(j).val().length == 0) {
$('input').eq(j).focus();
if (j == 1) {
$('input').eq(j).parent().next()
.next("div").text("此处不能为空");
$('input').eq(j).parent().next()
.next("div").css("color", 'red');
e.preventDefault();
return;
}
$('input').eq(j).parent().next(".tips").text(
"此处不能为空");
$('input').eq(j).parent().next(".tips").css(
"color", 'red');
e.preventDefault();
return;
}
}
document.formAdd.submit();
}
</script>
//注册按钮
<button type="button" class="btn btn-primary" onclick="check()" id="submit_btn">注册</button>