开发者

jQuery validation with Textbox naming on ASP.NET MVC page?

开发者 https://www.devze.com 2022-12-19 18:53 出处:网络
I have two Textbox controls on my view. Here jQuery validation function is working when the name is one word.

I have two Textbox controls on my view. Here jQuery validation function is working when the name is one word. It is not working when the na开发者_Go百科me is having any dots in it.

In the below jQuery function, if I use "lastname" textbox id for validation, it is not allowing any other keys except digits. But somehow if I use "AddressDetail.ZipCode" textbox id to allow only digits, it is not calling. I have to use only "AddressDetail.ZipCode" as it is referencing the child table.

Here is my code.

<table>
    <tbody>

<tr><td class="Form_Label"><label for="LastName">Last Name</label><em>*</em></td>
        <td CssClass="Form_Value"><%= Html.TextBox("lastname", Model.LastName, new { @class = "required", size = "30" })%></td></tr>

      <tr><td class="Form_Label"><label for="Zip">Zip</label><em>*</em></td>
        <td CssClass="Form_Value"><%= Html.TextBox("AddressDetail.ZipCode", Model.AddressDetail.FirstOrDefault().ZipCode, new { @class = "required zip", minlength = "5"})%></td></tr>

<tr><td colspan="2" align="center"><input type="submit" id="btnUpdProfile" value="Update" /> </td></tr>

JQuery function validation:

<script type="text/javascript">


    $(document).ready(function() {
        //Disabling all other keys except digits
        $('#AddressDetail.ZipCode').live('keypress', function(e) {
            var keyPressed;
            if ((e.charCode) && (e.keyCode == 0))
                keyPressed = e.charCode
            else
                keyPressed = e.keyCode;
            if (keyPressed < 48 || keyPressed > 57) {
                return false;
            }
            return true;
        });
    });

</script> 

    </tbody>
</table>


The . is interpreted as a class selector. You want:

$('#AddressDetail\\.ZipCode')

The double backslash is evaluated by the parser as '#AddressDetail\.ZipCode'. jQuery sees the backslash in front of the period and interprets it literally, instead of as a class selector.

0

精彩评论

暂无评论...
验证码 换一张
取 消