0

I need to validate a form and I want every input must have 6 more than characters.

The following script is working for 0 characters.

How do I edit it in order to set a minimum characters limit to 6?

<script>
    function validateForm(formId)
    {
        var inputs, index;
        var form=document.getElementById(formId);
        inputs = form.getElementsByTagName('input');
        for (index = 0; index < inputs.length; ++index) {
            // deal with inputs[index] element.
            if (inputs[index].value==null || inputs[index].value=="")
            {
                alert("Field is empty");
                return false;
            }
        }
    }

Here is the form:

<form name="myForm" id="myForm" method="post" action="" onsubmit="return validateForm('myForm');" style="margin-bottom:10px;" _lpchecked="1">
    <div id="InputsWrapper">
        <div>
            <input type="text" name="mytext[]" id="field_1" value="http://">
            <a href="#" id="AddMoreFileBox" style="font-size:12px;margin:0 0 0px 0;"><img src="images/plus.png" style="margin-left:10px;height:24px;">
            </a>
        </div>
    </div>
    <div style="height:10px;"></div>
    <input type="submit" style="border:none;background-color:#ff9900;padding:10px 30px 10px 
     30px;font-size:24px;font-weight:bold;color:#FFF;" value="CONTINUE">
</form>
2
  • if(inputs[index].value.length <6) Commented Oct 17, 2014 at 17:54
  • 2
    please format your html before you post is. using jsbeautifier.org. I format your codes. Commented Oct 17, 2014 at 18:01

3 Answers 3

2

Inside the loop, check the value length

if (inputs[index].value==null || inputs[index].value=="" || inputs[index].value.length < 6) {
    alert("Field is not 6 characters minimum!");
    return false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can check if a field has 6 characters using:

if (document.forms["name"]["field name"].value < 6).

Comments

0

I would suggest assigning a CSS class ("InputClassName" in this example) to the inputs you want to validate against - and then changing your code to something like this:

inputs = form.getElementsByClassName('InputClassName');
 for (index = 0; index < inputs.length; ++index) {
        if (inputs[index].value==null || inputs[index].value=="" || inputs[index].value.length < 6))
        {
            alert("Field must be 6 characters or more");
            return false;
        }
    }

The advantage of this approach is that you can add radio buttons or checkboxes WITHOUT the "InputClassName" class - that have values less than 6 characters and your logic will still work. With the current setup, your logic would break.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.