jquery textbox on keypress conditional validation
<p>I am working on asp.net mvc website and I have 4 dropdowns and 4 textboxes like this</p>

<pre><code>dialprefix1   callfromnumber1
dialprefix2   callfromnumber2
dialprefix3   callfromnumber3
dialprefix4   callfromnumber4
</code></pre>

<p>I want that If dialprefix1 dropdown has value 44 then callfromnumber1 should</p>

<p>have numberic value -- done
should start with 0 0r 1,2,7,8 otherwise show alert ( not working)
if number starts with 0 then length allowed in callfromnumber1 is 10 (working)
if number starts with 1,2,7,8 then length allowed in callfromnumber1 is 9 (working)</p>

<p>I have coded it like this:</p>

<pre><code>$(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 &amp;&amp; event.ctrlKey === true) ||
        // Allow: home, end, left, right
            (event.keyCode &gt;= 35 &amp;&amp; event.keyCode &lt;= 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 &lt; 48 || event.keyCode &gt; 57) &amp;&amp; (event.keyCode &lt; 96 || event.keyCode &gt; 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 &lt;= 10
            else
                return this.value.length &lt;= 9
        }
    })

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


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

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

});
</code></pre>

<p>Also, Can I make one keypress event for all four ( right now I have four keypress at the end of this snippet.</p>

<p>[Edit]</p>

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

<p>Please suggest</p>
