Click here to Skip to main content
15,936,982 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

to all,

i need a validation to my textbox, that accept only four integer. if user enter only two number or more than four it would say wrong number enter.

so if use enter four number it accept.

If user enter three number and one space than also it would raise message wrong number enter..


Use this validation using regex.

Share this answer
balongi 17-Apr-12 10:26am    
if user add number with space than also it acceptable. can you modify according to that. means four number and many space will acceptable
[no name] 24-Apr-12 3:32am    
sorry for late reply.
You can use \s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s* expression for allow spaces.
try this link[^]

you can also use this code and add only one or 2 line of code according to you requiremnest

<script type ="text/javascript">
    function MaskMoney(evt)
    if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57))) return false;
    var parts = evt.srcElement.value.split('.');
    if (parts.length > 4) return false;
    if (evt.keyCode == 46) return (parts.length == 1);
    if (parts[0].length >= 14) return false;
    if (parts.length == 4 && parts[1].length >= 4) return false;

In aspx.cs page

 protected void Page_Load(object sender, EventArgs e)
            TextBox1.Attributes.Add("onKeydown", "return MaskMoney(event)");
Share this answer
You can use \s*[0-9]\s*[0-9]\s*[0-9]\s*[0-9]\s* expression for allow spaces.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900