在下面的代码中,我有一个javascript函数和一个文本框。验证工作正常。我的目标是如果验证失败,它应该清除文本框值,光标应该在同一个文本框中,它不应该移动到其他控件。
.JS:
function ValidateRegExp(txtInput, REGEXP) {
var mySplitResult = new Array();
mySplitResult = REGEXP.split("~~");
var iReturn = 0;
for (i = 0; i < mySplitResult.length - 1; i++) {
var re = new RegExp(mySplitResult[i]);
if (!txtInput.match(re)) {
iReturn = iReturn + 1;
}
}
if (iReturn > 0) {
alert("Failed...");
} else {
alert("Success...");
}
}
代码隐藏:
txtField.Attributes.Add("onblur", "javascript:ValidateRegExp(document.getElementById('" + txtField.ClientID + "').value, '" + hidRegExp.Value + "');");
我不知道
为什么它对我不起作用,这行得通,我刚刚通过 2 个文本框创建了一个简单的例子
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtField" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
这是我的剧本
<script type="text/javascript">
function validate() {
var txt = document.getElementById("txtField");
if (txt.value === "" || txt.value.length <= 1) {
txt.value = ''; // to clear the textbox value
txt.focus(); // focus on the textbox
}
}
</script>
这是在aspx.cs
文件中
protected void Page_Load(object sender, EventArgs e)
{
txtField.Attributes.Add("onblur", "javascript:validate()");
}
在离开事件或模糊甚至每个控件的模糊中,您都可以调用 validate() 函数。有一个属性侧重于无效输入。因此,它将粘在文本框中,直到输入有效数据为止。
http://jqueryvalidation.org/validate/