I got few types and based on those types input string's length can be different. I have written this script for my client and when I got the task then I was aware that the task is not difficult but how best to do it. I do not had much time to think about it. This script was going to be used as fill up form so there was nothing much to think about performance etc. I think I got the nice idea in mind and written a short cool script. So, I am sharing it. If you have better way to do the same task then please share it in comment. Do not just go away every time after reading my post! Share something.
Here is a sample form with the required input fields for our case:
I do not have real scenario other than that I worked for client. So, my form looks like a hypothetical form. But anyway it will serve our purpose.
Now, user will select his choice - Type a, b, c or d. Based on that input string length will vary. The user know this type and based on that he should enter correct value as it will affect him/her. But we just want to assist in filling the form correctly. This is what we normally do in validation. In our case, we are not going to think of input format but only the length of the input string for each type.
The approach used should work in any programming language. I am interested in other approach you have for this validation.
Here is the script for the above validation:
Inside function, typeLen array tells about the possible input text length a type can take.
Hope it will be of little use to someone!