【发布时间】:2017-03-24 09:29:09
【问题描述】:
任何人都可以给我一个建议,我在检查时对我的 jQuery 登录验证有问题。
例如:
第一个问题: 我提交了我的表单,它有一个错误将出现在我的文本框下方并会显示,如果我再次提交,它将在第一条错误消息下方添加另外一条错误消息。
问题 1 如何摆脱那些重复的 = p class="text-danger"?
here is my username textbox
The username field is required. //error if empty
The username field is required. //2nd attempt of submit if textbox is empty
here is my password textbox
The password field is required. //error if empty
The username field is required. //2nd attempt of submit if textbox is empty
第二个问题: 我提交的表格都是空的,它将显示以下消息。然后,当我将值放在“用户名文本”上时,它将删除我用户名上的错误消息,这是正确的。但是,如果两者都是空的,并且当我只在密码文本框上输入值时,它将删除错误消息,因为它必须只删除密码错误。
问题二如何解决用户名有值且不为空时的错误提示?
here is my username textbox
The username field is required. //error if empty (if I put value on the username field it will removed this error message)
here is my password textbox
The password field is required. //error if empty (if i put value on the password field it will removed both error message)
登录表格:
<div class="form-group form-margin">
<label class="col-lg-4 control-label">Username:</label>
<div class="col-lg-7">
<?=form_input(['name'=>'username', 'class'=>'form-control error', 'autofocus'=>'autofocus', 'id'=>'username', 'placeholder'=>'Username']);?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Password:</label>
<div class="col-lg-7">
<?=form_password(['name'=>'password', 'class'=>'form-control error', 'id'=>'password', 'placeholder'=>'Password']);?>
</div>
</div>
<div class="row">
<div class="col-lg-10 col-lg-offset-1">
<?=form_button(['class'=>'btn btn-primary form-control', 'id'=>'btnSubmit', 'content'=>'Sign In'])?>
</div>
</div>
jquery:
$('#btnSubmit').click(function(e){
e.preventDefault();
var url = $('#loginForm').attr('action');
var data = $('#loginForm').serialize();
$.each($('input'), function(){
if($(this).val().length == 0){
$(this).parent().addClass('has-error');
$(this).append().after('<p class="text-danger">The ' + this.id + ' field is required.</p>');
} else {
$(this).parent().removeClass('has-error').addClass('has-success');
$('.text-danger').remove().after(this.id);
}
});
});
【问题讨论】:
标签: jquery validation