Javascript: Validate That Input String Length Falls In Range For Different Types

I am not sure what should be the title of the this post. I thought of titles like: JavaScript compare number in between array, check values in between min and max etc then thought of the long title. Still I am not sure if title is appropriate!

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:

Select Type:
Input Value:

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:

<script type="text/javascript">
function typeValidate(inputType, inp)
    var isValid = [];
    // Various types and based on that different input text's length possible
    var typeLen = new Array();
    typeLen['a'] = [10]; // min length = max lenght = 10
    typeLen['b'] = [10];
    typeLen['c'] = [8,9];
    typeLen['d'] = [3,8]; // min length = 3, max length = 8
    // Validation
    if (typeLen[inputType].length == 1)
        if (inp.length == typeLen[inputType][0] )
            isValid = [true,''];
            isValid = [false, 'Input type should be ' + typeLen[inputType][0] + 'chars long.'];
    else {
        if (inp.length >= typeLen[inputType][0]  && inp.length <= typeLen[inputType][1] )
            isValid = [true, ''];
           isValid  = [false, 'Input type should be ' + typeLen[inputType][0] + ' to ' + typeLen[inputType][1] + ' chars long. '];
    return isValid;
//Now, use the above function.
InpType = document.getElementById('InputType')
var InpType = InpType.options[InpType.selectedIndex].value;
var inpVal = document.getElementById('inpValue');
var typeCheck = typeValidate(InpType, inpVal);
if (typeCheck[0] == false)
    err = true;
    msg = 'Please check that you ...';
    msg = msg + typeCheck[1];

Inside function, typeLen array tells about the possible input text length a type can take.

Hope it will be of little use to someone!

Comments are open for an year period. Please, write here on Facebook page.