以下是我的视图页面,
<table class="width_full cellspace" border="0" cellspacing="0" cellpadding="0" style="vertical-align:top">
<tr>
<td colspan="3">
<strong>@Html.Label(Model.objTRSkillGradeLabelEntities.lblBPBlockedfor)</strong>
</td>
</tr>
<tr style="font:11px/20px Arial, Helvetica, sans-serif">
<td style="width: 33%;">
AccountId
<span class="redFont">*</span> :
<input type="text" id="txtBlkAccountId" />
<label id="lblAccountId" class="error"> </label>
</td>
</tr>
<tr>
<td colspan="3" class="textCenter">
<input class="btnInput" type="submit" value=@Model.objTRSkillGradeLabelEntities.btnBPSubmit onclick="javascript:UpdateTRData();" />
</td>
</tr>
</table>function UpdateTRData() {
var errorflag;
document.getElementById('lblAccountId').innerText = "";
var BlkAccountIdV = $('#txtBlkAccountId').val();
if (BlkAccountIdV == "" || BlkAccountIdV == null) {
document.getElementById('lblAccountId').innerText = "Enter AccountID";
errorflag=1;
}
var txtLength = $("#txtBlkAccountId").val().length;
if (txtLength != 7) {
document.getElementById('lblAccountId').innerText = "Enter valid AccountID";
errorflag = 1;
}
var i;
s = txtLength.toString();
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (isNaN(c)) {
document.getElementById('lblAccountId').innerText = "Enter valid AccountID";
errorflag = 1;
}
}
if (errorflag == 1) {
return false;
}
var AssociateID = $('#lblAssoID').text();
var BlkAccountId = $('#txtBlkAccountId').val();
$.ajax({
url:"@Url.Action("UpdateBlockDetails", "TravelReady")",
data:{
strAssociateID: AssociateID,
strBlkAccountId: BlkAccountId
},
dataType: "json",
type: "POST",
error: function(e) {
alert(e.getException().getMessage());
alert("An error occurred.");
},
success: function(data) { return data; }
});
}我已经验证了帐户Id文本框,只接受7位数字。单击该按钮后,此验证将生效。我需要再给一个验证,比如,不能输入超过20个数字。也就是说,如果我输入21位数字,应该会显示警告框。如何使用jQuery执行此验证?
发布于 2013-08-21 18:32:38
您可以使用文本框maxlength属性。
<input type="text" name="txtBlkAccountId" maxlength="20">或
您可以使用JS:
$("#txtBlkAccountId").on('keypress',function(){
if($(this).val().length>20){
alert("error message");
return false;
}
});https://stackoverflow.com/questions/18355016
复制相似问题