views:

87

answers:

3

I have a Abc field in the database and I want to make sure that the user doesnot cross the limit of the field in database, therefore I use validation that would say "You cannot enter more than 50 characters".

Now the question is that because the memory to store Chinese,Indic script and other such languages take more than 1 byte how would I validate the length.

What happens right now is that user has input of 30 characters and he gets a display message saying "You cannot enter more than 50 characters".

for solution what I know is I can increase the size of the database or check the byte length but it doesn't seems to be a good Idea.

What approach would you follow to get it done?

+3  A: 

Use a nvarchar type in your DB it limits number of characters and not number of bytes.

Guillaume
+1  A: 

If you're using .Net and SQl Server's nvarchar field, you should be fine I think.

Nvarchar stores 2 bytes per char anyway, so should accommodate the unicode string .net will pass to it with no problem. (in other words, the length in .Net and SQL are both measured in chars, not bytes, if you're using Nvarchar in the database).

Andrew M
+3  A: 

If your GUI is in WinForms or WPF then you should be able to set a maximum length on a text field which nicely stops people entering more than the specified no of characters, e.g.

myTextBox.MaxLength = 50

But you can set this from the VS Designer - select the textbox and go to its MaxLength property

Calanus
You can probably do this for ASP.Net forms as well, although I've not checked....
Calanus
@Calanus: this solution plus nvarchar as other suggest and any situation should be well taken care of. +1 for forms example.
dboarman