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

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..

thanks!
Posted

Use this validation using regex.

[0-9][0-9][0-9][0-9]
 
Share this answer
 
Comments
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    
Hi,
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

http://bytes.com/topic/javascript/answers/90985-validate-form-4-digit-integer[^]

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


XML
<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;
    }
    </script>


In aspx.cs page
-----------------

 protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Attributes.Add("onKeydown", "return MaskMoney(event)");
        }
 
Share this answer
 
v2
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