Javascript 验证引导模式中的输入文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27968361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate input text in bootstrap modal
提问by mkab
I want to validate a form in a bootstrap modal without using any framework. It's actually a simple modal with only an input text and two buttons "Close" and "Send". The user should type his/her name in the input box and click send. The form is the sent with the method post.
我想在不使用任何框架的情况下以引导模式验证表单。它实际上是一个简单的模式,只有一个输入文本和两个按钮“关闭”和“发送”。用户应在输入框中键入他/她的姓名,然后单击发送。表单是使用方法 post 发送的。
What I want to do is that, if the user doesn't enter anything in the input box of and clicks on the button "Send", the input box should have a red border circling it instead of the default blue border. Here's the code for my modal:
我想要做的是,如果用户没有在输入框中输入任何内容并单击“发送”按钮,则输入框应该有一个红色边框环绕它而不是默认的蓝色边框。这是我的模态代码:
<div id="myModal" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!--form-->
<form class = "form-horizontal" id="myForm" method="post" action="test.php" role="form">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">My modal</h4>
</div>
<div class="modal-body">
<p>
Please enter your name:
</p>
<br/>
<div class="form-group">
<div class="col-lg-12">
<label class="control-label" for="firstname">Name</label>
<input type="text" class="form-control required" id="firstname" name="firstname">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="myFormSubmit" class="btn btn-primary">Send</button>
</div>
</form>
</div>
</div>
</div>
I tried using javascript to change the class of the input text to has-errorbut it doesn't produce the red outline. When I click on send it sends the empty value through the post method instead of
Here's my javascript code:
我尝试使用 javascript 将输入文本的类更改为,has-error但它不会产生红色轮廓。当我点击发送时,它通过 post 方法发送空值,而不是这是我的 javascript 代码:
<script type="text/javascript">
$('#myForm').validate({
rules: {
name: {
minlength: 1,
required: true
},
},
highlight: function (element) {
$('#firstname').removeClass('has-success').addClass('has-error');
},
success: function (element) {
$('#firstname').removeClass('has-error').addClass('has-success');
}
});
</script>
I feel like I'm mixing a whole lot of things here. I'm still new to bootstrap and javascript. How can I go about getting the desired red outline? Thanks.
我觉得我在这里混合了很多东西。我对 bootstrap 和 javascript 还是个新手。我怎样才能获得所需的红色轮廓?谢谢。
Edit: Validate function:
编辑:验证功能:
function validate() {
var x = document.forms["myForm"]["firstname"].value;
if (x == null || x == "") {
return false;
}
}
回答by Plamen Nikolov
You need to change the CSSclasses of the inputparentelement div.form-group, not the inputitself:
您需要更改父元素的CSS类,而不是它本身:inputdiv.form-groupinput
The HTMLafter the submit button click should look like this:
在HTML提交按钮点击后应该是这样的:
<div class="form-group has-error">
<div class="col-lg-12">
<label class="control-label" for="firstname">Name</label>
<input type="text" class="form-control required" id="firstname" name="firstname">
</div>
</div>
To achieve this, alter your JavaScriptcode to this:
为此,请将您的JavaScript代码更改为:
<script type="text/javascript">
$('#myForm').on('submit', function(e) {
var firstName = $('#firstname');
// Check if there is an entered value
if(!firstName.val()) {
// Add errors highlight
firstName.closest('.form-group').removeClass('has-success').addClass('has-error');
// Stop submission of the form
e.preventDefault();
} else {
// Remove the errors highlight
firstName.closest('.form-group').removeClass('has-error').addClass('has-success');
}
});
</script>
回答by Minesh
div in boot strap...
引导带中的 div...
<div class="form-group">
<div class="row">
<label class="col-xs-2 col-sm-2 control-label">city:</label>
<div class="col-xs-3 col-sm-3 formGroups"
id="city_form1">
<form:input name="city" class="form-control"
placeholder="City" data-toggle="tooltip"
data-placement="bottom" title="City" maxlength="15" />
<small class="help-block col-sm-offset-0 col-sm-9"
style="display: none;">city must require</small>
</div>
</div>
</div>
as you see there is .help-block is display: none now you write javascript function and check validation and if validation now pass make it display: block ... that's way you can do
如您所见,有 .help-block is display: none 现在您编写 javascript 函数并检查验证,如果验证现在通过,则使其显示:阻止...这是您可以做的
enjoy with boot strap :)
享受与引导带:)
回答by Minesh
its very easy
这很容易
just apply class .has-error to div by javascript function
只需通过 javascript 函数将 class .has-error 应用到 div
<div class="form-group has-error">
<div class="row">
<label class="col-xs-2 col-sm-2 control-label">city:</label>
<div class="col-xs-3 col-sm-3 formGroups"
id="city_form1">
<form:input name="city" class="form-control"
placeholder="City" data-toggle="tooltip"
data-placement="bottom" title="City" maxlength="15" />
</div>
</div>
</div>
回答by stephen carvalho
Just use required inside the input field like this
只需在输入字段中使用 required 就像这样
<input type="text" class="form-control required" id="firstname" name="firstname" required/>

