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


i need to validate a input that is taken by text box
that entry should be like 4digits the '-' then 1digit '-'then anything else means its would alpha ar numeric but over limit of 15digit

for Example-

"2012-1-no112"

i tried this but not working.......

C#
Regex FilenameRegex = null;
        FilenameRegex = new Regex("^[0-9]{4}-[0-9]{1}-[[0-9a-zA-Z]*]$", RegexOptions.IgnoreCase);
        if (!FilenameRegex.IsMatch(txtFromNo.Text))
        {
            msg.Show("Please Enter with this Format : 2012-1-as121");
            return;
        }


Thanks in advance...
Posted

You have an error in you Regex:
Remove those double square brackets in the last part of your Regex ;)

This should work: ^[0-9]{4}-[0-9]{1}-[0-9a-zA-Z]*$"

Hint: To test your Regex this is a handy site: http://regexpal.com/[^]


Btw. I don't really know what you mean by
Shubh Agrahari wrote:
then anything else means its would alpha ar numeric but over limit of 15digit
Do you want to limit the whole string to max 15 chars. or the last part can be 0-15 chars? or has the last part to be longer longer than 15 chars?
   
v2
Comments
Shubh Agrahari 15-Nov-13 5:58am
   
i tried that also(removed []) but still not working and limit can be 0-15 chars
Shubh Agrahari 15-Nov-13 6:01am
   
thanks..there is a bit mistake now fine
Nicholas Marty 15-Nov-13 6:08am
   
So if you want to limit the whole string to 15 chars just replace the asterisk "*" with "{0,8}"
Shubh Agrahari 15-Nov-13 7:26am
   
thanks
Hi Shubh,

Now try this expression ^[0-9]{4}-[0-9]{1}-[0-9a-zA-Z]*$
Now its working sooooper.
You can test it in http://www.rubular.com/

Hope this helps you a bit.

Regards,
RK
   
v2
Comments
Shubh Agrahari 15-Nov-13 6:02am
   
thanks..there is a bit mistake now fine
♥…ЯҠ…♥ 15-Nov-13 6:04am
   
it happens... because we are human....
Shubh Agrahari 15-Nov-13 7:26am
   
ya its true

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