How to restrict the textbox length is only 10 numbers in MVC ?

Posted by Manimaddu under ASP.NET MVC on 8/8/2014 | Points: 10 | Views : 18047 | Status : [Member] | Replies : 4
How to restrict the textbox length is only 10 numbers in MVC ?

Thanks & Regards,
Mani Kumar



Responses

Posted by: kgovindarao523-21772 on: 8/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Take an input text box:
<input type="text" maxlength="10" onkeydown="numericOnly(this,event)" />

and here is javascript code:
function numericOnly(element,e)
{
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
}


Thank you,
Govind

Manimaddu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/8/2014 [Member] [MVP] Platinum | Points: 25

Up
0
Down
<%=Html.TextBox("polNum",null, new {maxlength=10}) %>


References:
http://stackoverflow.com/questions/1928651/how-do-i-limit-the-length-of-characters-in-a-textbox-in-mvc
http://www.aspdotnet-suresh.com/2010/12/how-to-restrict-user-to-enter-only.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Manimaddu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 8/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

If you are using Data Annotations in MVC:
Place the attribute on class property.

[Range(0, 9999999999, ErrorMessage="Invalid Number")]
public int Number { get; set; }


Thank you,
Govind

Manimaddu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 8/8/2014 [Member] [MVP] Platinum | Points: 25

Login to post response