JS+后台验证数据是否存在于数据库中
<asp:TemplateField HeaderText="所屬年月">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Width="90%" CssClass="TextBox" MaxLength="5"
Text='<%# Eval("Var_Date") %>' onblur="this.value=this.value.replace(/\D/g,'');ChkVatInvoice(this,'vatDate');"></asp:TextBox>
</ItemTemplate>
<HeaderStyle Width="6%" />
</asp:TemplateField>
function ChkVatInvoice(obj, val) {
var txtVal = "";
var vatType = "";
var vatDate = "";
if (val == "txt") {
txtVal = $(obj).val();
vatType = $(obj).parent().prev().children().first().val();
vatDate = $(obj).parent().prev().prev().prev().prev().children().first().val();
}
else if (val == "select") {
txtVal = $(obj).parent().next().children().first().val();
vatType = obj.value;
vatDate = $(obj).parent().prev().prev().prev().children().first().val();
}
else {
txtVal = $(obj).parent().next().next().next().next().children().first().val();
vatType = $(obj).parent().next().next().next().children().first().val();
vatDate = $(obj).val();
}
if (txtVal != "") {
if (txtVal.length < 2) {
alert("<%= localize.UnifiedNo + localize.IsNotValid %>"); //统编格式无效。
}
else {
//格式代码
var char1 = txtVal.substr(0, 1);
var char2 = txtVal.substr(1, 1);
if (vatType == "21" || vatType == "23" || vatType == "25") {
//前两位必须是英文且要存在于发票字轨
if (char1.charCodeAt() >= 65 &&
char1.charCodeAt() <= 90 &&
char2.charCodeAt() >= 65 &&
char2.charCodeAt() <= 90) {
//检测是否存在发票字轨
var xmlURL = "JsonData.ashx?t=IT&val=" + txtVal.substr(0, 2) + "&vatType=" + vatType + "&vatDate=" + vatDate;
$.get(xmlURL, {}, function(result) {
if (result == "N") {
alert("<%=localize.lblVatInvoice %>\"" + txtVal.substr(0, 2) + "\"<%= localize.IsNotExistsInInvoice%>"); //发票号码 不存在於发票字轨。
if (val == "txt") {
obj.value = '';
obj.focus();
}
}