按键条件验证的jquery文本框

时间:2013-01-12 08:40:10

标签: javascript jquery

我正在使用asp.net mvc网站,我有4个下拉菜单和4个这样的文本框

dialprefix1   callfromnumber1
dialprefix2   callfromnumber2
dialprefix3   callfromnumber3
dialprefix4   callfromnumber4

我希望如果dialprefix1下拉列表的值为44,则callfromnumber1应为

有数字值 - 完成 应该从0 0r 1,2,7,8开始,否则显示警报(不工作) 如果number以0开头,那么callfromnumber1中允许的长度为10(工作) 如果数字以1,2,7,8开头,那么callfromnumber1中允许的长度为9(工作)

我的编码是这样的:

$(document).ready(function () {

    $("#DefaultCallFrom1, #DefaultCallFrom2, #DefaultCallFrom3, #DefaultCallFrom4").keydown(function (event) {
        // Allow: backspace, delete, tab, escape, and enter
        if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
        // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) ||
        // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
            // let it happen, don't do anything
            return;
        }
        else {
            // Ensure that it is a number and stop the keypress
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
                event.preventDefault();
            }
        }

    });

    if ($("#DefaultCallFrom1").val().match("5")) {
        displayAlertMessage($("#DefaultCallFrom1").val().substring(0, 1) + "Number must start with 0, 1, 2, 7 or 8");
    }

    $("#DefaultCallFrom1").keypress(function () {
        if ($("#Dialprefix1").val() == "44") {           
            if (this.value.substring(0, 1) == "0")
                return this.value.length <= 10
            else
                return this.value.length <= 9
        }
    })

    $("#DefaultCallFrom2").keypress(function () {
        if ($("#Dialprefix2").val() == "44") {
            if (this.value.substring(0, 1) == "0")
                return this.value.length <= 10
            else
                return this.value.length <= 9
        }
    })


    $("#DefaultCallFrom3").keypress(function () {
        if ($("#Dialprefix3").val() == "44") {
            if (this.value.substring(0, 1) == "0")
                return this.value.length <= 10
            else
                return this.value.length <= 9
        }
    })

    $("#DefaultCallFrom4").keypress(function () {
        if ($("#Dialprefix4").val() == "44") {
            if (this.value.substring(0, 1) == "0")
                return this.value.length <= 10
            else
                return this.value.length <= 9
        }
    })

});

另外,我可以为所有四个人制作一个按键事件(现在我在这个片段的末尾有四个按键。

[编辑]

<td style="padding-left: 1px">
    <input type="text" value="1" readonly="readonly" name="srNo" id="srNo" disabled="disabled" class="tiny" style="text-    align:right">
    <select name="Dialprefix1" id="Dialprefix1">
        <option value="44">+44</option>
        <option value="001">+001</option>
        <option value="11" selected="selected">+11</option>
        <option value="12">+12</option>
        <option value="13">+13</option>
    </select>
    <input type="text" value="" name="DefaultCallFrom1" id="DefaultCallFrom1" class="textarea normal" autocomplete="off">
    <br>
    <input type="text" value="2" readonly="readonly" name="srNo" id="srNo" disabled="disabled" class="tiny" style="text-    align:right">
    <select name="Dialprefix2" id="Dialprefix2">
        <option value="44">+44</option>
        <option value="001">+001</option>
        <option value="11" selected="selected">+11</option>
        <option value="12">+12</option>
        <option value="13">+13</option>
    </select>
    <input type="text" value="" name="DefaultCallFrom2" id="DefaultCallFrom2" class="textarea normal" autocomplete="off">
    <br>
    <input type="text" value="3" readonly="readonly" name="srNo" id="srNo" disabled="disabled" class="tiny" style="text-    align:right">
    <select name="Dialprefix3" id="Dialprefix3">
        <option value="44">+44</option>
        <option value="001">+001</option>
        <option value="11" selected="selected">+11</option>
        <option value="12">+12</option>
        <option value="13">+13</option>
    </select>
    <input type="text" value="" name="DefaultCallFrom3" id="DefaultCallFrom3" class="textarea normal" autocomplete="off">
    <br>
    <input type="text" value="4" readonly="readonly" name="srNo" id="srNo" disabled="disabled" class="tiny" style="text-    align:right">
    <select name="Dialprefix4" id="Dialprefix4">
        <option value="44">+44</option>
        <option value="001">+001</option>
        <option value="11" selected="selected">+11</option>
        <option value="12">+12</option>
        <option value="13">+13</option>
    </select>
    <input type="text" value="" name="DefaultCallFrom4" id="DefaultCallFrom4" class="textarea normal" autocomplete="off">
</td>

请建议

0 个答案:

没有答案