views:

256

answers:

1

Hi

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 name is having any dots in it.

In the below JQuery fucntion, 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>

Appreciate your responses.

+2  A: 

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.

CalebD
`$('#AddressDetail\\.ZipCode')` two are needed iirc
Paul Creasey
Correct and edited!
CalebD
hmm..... i tried. Still not working.
Rita
Not sure. It is generating the HTML as AddressDetail_ZipCode. Now it works fine.
Rita