jQuery 单击按钮时的文本框验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33915655/
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
textbox validation on button click
提问by shifa fatima
I am working on simple website making a textbox and a button. while clicking on button error should be given that "textbox value can't be empty". i tried from many sites but my issue is not yet resolved. one important thing to mention this button and textbox is not enclosed in a form.
我正在制作一个简单的网站,制作一个文本框和一个按钮。点击按钮错误时应该给出“文本框值不能为空”。我从许多网站尝试过,但我的问题尚未解决。值得一提的是,此按钮和文本框未包含在表单中。
<input id="AccountText" type="text" name="AccountText"
class="form-control" placeholder="Name" required=''/>
<input id="AddAccount" type="button" class="btn btn-primary" value="Save" />
i have already tried controltovalidation but it is also not working for me.
我已经尝试过 controltovalidation 但它也不适合我。
回答by Chris
Try this. Should help you get started.
尝试这个。应该可以帮助您入门。
HTML
HTML
<input id="AccountText" type="text" name="AccountText" value="" class="form-control" placeholder="Name" required=''/>
//submit button
<input class="button" type="submit" value="submit">
JQUERY
查询
The basics: When the user clicks the submit button (using the .click()
function) jquery checks whether the input value is empty (using .val()
to get the value of the input.) If it is empty a message appears (using alert()
) prompting the user to complete the field
基础知识:当用户点击提交按钮时(使用.click()
函数)jquery 检查输入值是否为空(.val()
用于获取输入的值)。如果为空,则会出现一条消息(使用alert()
)提示用户完成场地
$(document).ready(function(){
$('.button').click(function(){
if ($('#AccountText').val() == ""){
alert('Please complete the field');
}
});
});
回答by Goran Mottram
If you are using Web Forms, you could make use of ASP.Net validation controls. Your markup will look something like the following:
如果您使用的是 Web 窗体,则可以使用ASP.Net 验证控件。您的标记将类似于以下内容:
<asp:TextBox ID="AccountText" runat="server" CssClass="form-control" placeholder="Name" />
<asp:RequiredFieldValidator ID="AccountTextValidator" runat="server"
ControlToValidate="AccountText"
Text="Textbox value can't be empty"
/>
<asp:Button ID="AddAccount" runat="server" Text="Save" CssClass="btn btn-primary" />
回答by Sanjay Kumar N S
Try this:
尝试这个:
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<input id="AccountText" type="text" name="AccountText"
class="form-control" placeholder="Name" required=''/>
<input id="AddAccount" type="button" class="btn btn-primary" value="Save" />
<script>
$(document).ready(function() {
$('#AddAccount').click(function() {
if (!$.trim($('#AccountText').val())) {
alert("textbox value can't be empty");
}
});
});
</script>
回答by RajeeshMenoth
Try this :
尝试这个 :
Jquery Reference :
jQuery 参考:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
HTML :
HTML :
<input id="AccountText" type="text" name="AccountText" class="form-control" placeholder="Name" required='' />
Jquery :
查询:
$(document).ready(function () {
$('#AddAccount').click(function () {
var inVal = $("#AccountText").val();
if (inVal.length == 0) {
alert("textbox value can't be empty");
$("#AccountText").focus();
}
});
});
});
Demo :Click here
演示:点击这里
回答by Asit Kumar
You Can Try one of the following:
您可以尝试以下方法之一:
1) make required='true' 2) If the textbox have some specific about what type of text to be entered, you can also put the regex pattern. 3) You can use this in your script:
1) make required='true' 2) 如果文本框对输入的文本类型有一些具体说明,您也可以放入正则表达式模式。3)您可以在脚本中使用它:
<script>
$(document).ready(function() {
$('#AddAccount').click(function() {
if (!$.trim($('#AccountText').val())) {
alert("Please enter the text");
}
});
});
</script>