0

Have the following READONLY textbox:

 @Html.TextBoxFor(model => model.ClientNumber, new {@readonly = "readonly", @class ="message-label"})

and Save button:

 <button type="submit" id="btnSave" name="Command" value="Save">Save</button>

When user clicks on Client Number text box and hits Enter, then submit behavior is invoked and next page is displayed after data is saved to the database.

How to change this, so that hitting Enter while in this field does nothing, similar to how page behaves for a usual label?

1 Answer 1

0

You can do this with a little bit of javascript, handling keydown event and checking whether Enter was hit:

$(document).ready(function() {
    $('input[name=ClientNumber]').keydown(function(e) {
        if (e.keyCode === 13) {
            e.preventDefault();
            return false;
        }
    });
});
4
  • I added this to scripts section; but for some reason, it did not help. What should I do to make it work? @section Scripts { @Scripts.Render("~/bundles/jqueryval") <script type="text/javascript"> $(document).ready(function () { $('input[name=ClientNumber]').keydown(function (e) { if (e.keyCode == 13) { e.preventDefault(); return false; } }); }); </script> } Oct 24, 2013 at 17:27
  • Thank you, it worked after I commented out @Scripts.Render("~/bundles/jqueryval") Oct 24, 2013 at 17:29
  • Do you know how to use this function along with @Scripts.Render("~/bundles/jqueryval") ? I am afraid to leave it commented out since would like to keep standard behavior for other fields? Oct 24, 2013 at 17:41
  • @SearchForKnowledge, currently it does not. Do you think it should? OP clearly had a different problem, which is "here is the textbox, we know it's readonly, please prevent enter hits". So he did not need this check. But I can add it to the answer, should be simple thing
    – Andrei
    May 20, 2015 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.