views:

200

answers:

3

Hi

I have an ASP.NET MVC Page along with Zipcode, Phonenumber & Fax.

Now i want to disable all other keys and only enable digits from the keyboard while typing on these textboxes.

Appreciate your responses.

As of now this is how the form is validated using Validation Plugin.

Zip*

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

$("#frmCustDetails").validate({

        rules: {
            ZipCode: {
                required: true, digits: true, minlength: 5, maxlength: 9 
         },
        messages: {
            ZipCode: {
                required: "please enter zipcode", digits: "please enter only digits", minlength: "Min is 5", maxlength: "Max is 9
       }
    });
+1  A: 

You can use a mask plugin, like this one. You should still validate it thought, both one the client side and on the server side.

Mattias Jakobsson
I m trying $("#ZipCode").mask("99999"); It is not working. Not sure if i am missing any other references I downloaded masked input plugin and referenced it.
Rita
I haven't actually used that plugin myself, so I don't think I can help you much with how it works. See if there is a example on the site that you can download to see what you are missing. Or you can just google for "jquery masked input" and you will get a lot of results (that's how I found that plugin just now).
Mattias Jakobsson
Okay. Sure will try that.
Rita
+1  A: 

To prevent certain characters from being entered into a textbox, you can attach an onkeypress event to the textbox, and return false if the input recieved did not meet your expectations:

function validNumber(e) {
    keyPressed = e.keyCode;
    if (keyPressed < 48 || keyPressed > 57) {
       return false;
    }

    return true;
}
Ryan Brunner
Mattias Jakobsson
I think the opposite is true. keyPressed can never be both less than 48 AND greater than 57 :) Keep in mind that I'm searching for numbers outside the range other than inside.
Ryan Brunner
Yes, sorry. Didn't think for to long there :)
Mattias Jakobsson
This worked. Thanks Ryan.
Rita
A: 

Just FYI: keypress always returns 0 in FF. Please use keydown instead.

FALCONSEYE