[英]Validating input length using JavaScript
I want to show an error if user enters less than 4 letters in text box. 如果用户在文本框中输入少于4个字母,我想显示一个错误。 Here is the code I'm currently using now, it is somewhat working. 这是我当前正在使用的代码,它有些正常。 The problem is - it is showing the error and then it is submitting the value also. 问题是-它显示错误,然后还提交了值。
I want to stop it submitting if user enters less than 4 letters. 如果用户输入的字母少于4个,我想停止提交。
JavaScript JavaScript的
<script type="text/javascript">
function check(what)
{
var length=what.search_text.value.length;
if (length >3 && length < 21)
what.submit();
else
alert("Your nick should be 4-20 characters long.");
}
</script>
HTML HTML
<form method=post action=''><input type=hidden name=todo value=search>
<p align=center>
Enter Your Name:
<input type=text name=search_text value='$search_text'>
<input onclick=check(this.form); return false; type=submit value=Search>
</p>
</form>
in case of invalid values you need to 如果值无效,则需要
return false;
so that form submission is cancelled 这样就取消了表单提交
<script type="text/javascript">
function check(what)
{
var length=what.search_text.value.length;
if (length >3 && length < 21){
what.submit();
}
else {
alert("Your nick should be 4-20 characters long.");
return false;
}
}
</script>
<?php
echo "<form method='post' action='' onsubmit='return check(this);'><input type=hidden name=todo value=search>
<p align=center>Enter Your Name: <input type=text name=search_text value='$search_text'><input type=submit value=Search></p><br>
</form>";
?>
Changes : 变化 :
onsubmit='return check(this);'
added in form tag 在表单标签中添加 onclick
from button submit 从按钮提交中删除onclick
return false;
added in else condition of js function 在js函数的else条件中添加 Suggestion : Always try to use jQuery, It is simple and will reduce your code length. 建议 :始终尝试使用jQuery,这很简单,会减少您的代码长度。
u can achive this by using RegularExpressionValidator control 您可以通过使用RegularExpressionValidator控件来实现
design your textbox like this example i am using. 像我正在使用的这个示例一样设计您的文本框。
<asp:TextBox ID="txtCityCode" runat="server" Width="147px" CssClass="txt" MaxLength="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCityCode" runat="server" ErrorMessage="City Code is required information."
ControlToValidate="txtCityCode" Display="None" ValidationGroup="Save" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revCityCode" runat="server" ErrorMessage="Numbers and special characters not allowed in 'City Code' field."
ControlToValidate="txtCityCode" ValidationExpression="^[a-zA-Z\s]+$" ValidationGroup="Save"
Display="None" SetFocusOnError="True"></asp:RegularExpressionValidator>
Just Change the ValidationExpression according to your needs 只需根据您的需要更改ValidationExpression
<script type="text/javascript">
function check(what)
{
var length=what.search_text.value.length;
if (length >3 && length < 21)
{ what.submit(); return true; }
else
{alert("Your nick should be 4-20 characters long.");
return false;}
}
</script>
and in HTML
<input onclick='return check(this.form)' type="submit" value="Search">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.